All the parameters are optional. Also, there are no upper limits for the values, so you can overload any of the parameters.
{ms:1000000} // a million milliseconds{d:360} // 360 days
Special considerations for months and years
If the day of the month on the original date is greater than the number of days in the final month, the day of the month will change to the last day in the final month.
Example:
_date.date([2010, 0, 31]) // January 31_date.date([2010, 0, 31]).add({M:1}) // February 28
_date.date().subtract()
_date.date().subtract(object)
Functions the same as _date.date().add(), only using subtraction instead of addition.
Example:
_date.date([2010, 1, 28]) // February 28_date.date([2010, 1, 28]).subtract({M:1}) // January 28
_date.date().format()
_date.date().format(string)
Returns a human readable string based on the format string that was passed in.
The formats are created by creating a string of replacable characters.
Month
Input
Output
Month
M
1 2 ... 11 12
Mo
1st 2nd ... 11th 12th
MM
01 02 ... 11 12
MMM
Jan Feb ... Nov Dec
MMMM
January February ... November December
Day of Month
D
1 2 ... 30 30
Do
1st 2nd ... 30th 31st
DD
01 02 ... 30 31
Day of Year
DDD
1 2 ... 364 365
DDDo
1st 2nd ... 364th 365th
DDDD
001 002 ... 364 365
Day of Week
d
0 1 ... 5 6
do
0th 1st ... 5th 6th
ddd
Sun Mon ... Fri Sat
dddd
Sunday Monday ... Friday Saturday
Week of Year
w
1 2 ... 52 53
wo
1st 2nd ... 52nd 53rd
ww
01 02 ... 52 53
Year
YY
70 71 ... 29 30
YYYY
1970 1971 ... 2029 2030
AM/PM
A
AM PM
a
am pm
Hour
H
0 1 ... 22 23
HH
00 01 ... 22 23
h
1 2 ... 11 12
hh
01 02 ... 11 12
Minute
m
0 1 ... 58 59
mm
00 01 ... 58 59
Second
s
0 1 ... 58 59
ss
00 01 ... 58 59
Timezone
z
EST CST ... MST PST
zz
Eastern Standard Time ... Pacific Standard Time
NOTE: Internet Explorer uses a different implementation of
Date.toString(), so we are unable to retrieve the full string
of the timezone, and will fall back to 'z'.
So:
Firefox, Chrome, Safari, etc. == 'Eastern Standard Time'
Internet Explorer, etc. == 'EST'
Returns a string as relative time ('minutes ago', '5 months ago', etc).
You can pass anything that you would pass to _date.date() as the first parameter, or a _date.date() object.
_date.date([2007, 0, 29]).from(_date.date([2007, 0, 28])) // "a day ago");
You can pass true as the second parameter to return without the prefixes and suffixes.
_date.date([2007, 0, 29]).from(_date.date([2007, 0, 28]), true) // "a day");
You can pass true as the third parameter to return as milliseconds.
The number of milliseconds returned will be positive if the date passed
in is later than the first date, and negative if the date passed in is earlier.
A shortcut for _date.date().from(_date.now(), withoutSuffix:boolean, asMilliseconds:boolean)
_date.date().isLeapYear()
Returns true if the year is a leap year, false if it is not
Localization and Customization
To customize the wording of _date.date().format() and _date.date().from() you can use the _date.date().customize() function, passing in an
object with the parameters you wish to overwrite.
Added underscore.date to npm. Removed dependancies on underscore.
0.3.2
Added 'z' and 'zz' to _.date().format(). Cleaned up some redundant code to trim off some bytes.
0.3.1
Cleaned up the namespace. Moved all date manipulation and display functions to the _.date() object.
0.3.0
Switched to the Underscore methodology of not mucking with the native objects' prototypes.
Made chaining possible.
0.2.1
Changed date names to be a more pseudo standardized 'dddd, MMMM Do YYYY, h:mm:ss a'.
Added Date.prototype functions add, subtract, isdst, and isleapyear.
0.2.0
Changed function names to be more concise.
Changed date format from php date format to custom format.