nodejs日期时间插件Date-Utils

in 编程
关注公众号【好便宜】( ID:haopianyi222 ),领红包啦~
阿里云,国内最大的云服务商,注册就送数千元优惠券:https://t.cn/AiQe5A0g
腾讯云,良心云,价格优惠: https://t.cn/AieHwwKl
搬瓦工,CN2 GIA 优质线路,搭梯子、海外建站推荐: https://t.cn/AieHwfX9

前言

介绍一个nodejs非常实用的日期时间插件:Date-Utils

在nodejs中最常用的就是日期时间的格式化,但是nodejs本身实现不是很好,在网上找一番之后发现Date-Utils是最好用的,方便,简单易用。

简单效果实例

废话不多说,先看下效果:
格式化一个最常用的日期时间格式就是:yyyy-MM-dd HH:mm:ss<==>2015-09-22 09:40:49
其实输出这种格式的代码非常简单:

require('date-utils');
var dt = new Date();
console.log(dt.toFormat("YYYY-MM-DD HH24:MI:SS"));

安装

Using within a Browser

<script type="text/javascript" src="https://segmentfault.com/a/1190000003777624/date-utils.min.js"></script>

Using with Node.js

$ npm install -g date-utils
require('date-utils');

Note: This did not work in the REPL before Node.js 0.6 due to how Node.js handles context in the REPL.

API

来自:API Document

Class: Date
Date
new Date()
Methods
<static> compare(date1, date2) → {Number}
<static> equals(date1, date2) → {Boolean}
<static> equalsDay(date1, date2) → {Boolean}
<static> getDayNumberFromName(day) → {Number}
<static> getDaysInMonth(year, month) → {Number}
<static> getMonthAbbrNameFromNumber(month) → {String}
<static> getMonthNameFromNumber(month) → {String}
<static> getMonthNumberFromName(month) → {Number}
<static> isLeapYear(year) → {Boolean}
<static> today() → {Date}
<static> tomorrow() → {Date}
<static> UTCtoday() → {Date}
<static> UTCtomorrow() → {Date}
<static> UTCyesterday() → {Date}
<static> validateDay(day, year, month) → {Boolean}
<static> validateHour(hour) → {Boolean}
<static> validateMillisecond(millisecond) → {Boolean}
<static> validateMinute(minute) → {Boolean}
<static> validateMonth(month) → {Boolean}
<static> validateSecond(second) → {Boolean}
<static> validateYear(year) → {Boolean}
<static> yesterday() → {Date}
add(additions) → {Date}
addHours(hours) → {Date}
addMilliseconds(milliseconds) → {Date}
addMinutes(minutes) → {Date}
addMonths(months) → {Date}
addSeconds(seconds) → {Date}
addSeconds(days) → {Date}
addWeekdays(weekdays) → {Date}
addWeeks(weeks) → {Date}
addYears(years) → {Date}
between() → {Boolean}
clearTime() → {Date}
clearUTCTime() → {Date}
clone()
compareTo(Date) → {Number}
equals(Date) → {Boolean}
equalsDay(Date) → {Boolean}
getDaysBetween(Date) → {Number}
getFullWeekNumber() → {Number}
getHoursBetween(Date) → {Number}
getLastMonthName() → {String}
getMillisecondsBetween(Date) → {Number}
getMinutesBetween(Date) → {Number}
getMonthAbbr() → {String}
getMonthName() → {String}
getMonthsBetween(Date) → {Number}
getOrdinalNumber() → {Number}
getSecondsBetween(Date) → {Number}
getUTCOffset() → {String}
getWeekNumber() → {Number}
isAfter(Date) → {Boolean}
isBefore(Date) → {Boolean}
isToday() → {Boolean}
isWeekend() → {Boolean}
remove(removals) → {Date}
removeDays(days) → {Date}
removeHours(hours) → {Date}
removeMilliseconds(milliseconds) → {Date}
removeMinutes(seconds) → {Date}
removeMonths(months) → {Date}
removeSeconds(seconds) → {Date}
removeWeeks(weeks) → {Date}
removeYears(years) → {Date}
setTimeToNow()
toCLFString() → {String}
toDBString() → {String}
toFormat(format) → {String}
toUTCFormat(format) → {String}
toYMD(separator) → {String}
关注公众号【好便宜】( ID:haopianyi222 ),领红包啦~
阿里云,国内最大的云服务商,注册就送数千元优惠券:https://t.cn/AiQe5A0g
腾讯云,良心云,价格优惠: https://t.cn/AieHwwKl
搬瓦工,CN2 GIA 优质线路,搭梯子、海外建站推荐: https://t.cn/AieHwfX9
扫一扫关注公众号添加购物返利助手,领红包
Comments are closed.

推荐使用阿里云服务器

超多优惠券

服务器最低一折,一年不到100!

朕已阅去看看