前言

介绍一个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="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}

Seay
767 声望32 粉丝