Date utilities.
How many days elapsed since begining of the year (following gregorian calendar).
// Jan 1st
dayOfTheYear(new Date(2013, 0, 1)); // 1
// Dec 31th
dayOfTheYear(new Date(2013, 11, 31)); // 364
Calculate the difference between dates (range).
The returned value is always positive. The default unitName
is "ms"
.
Available units: year
, month
, week
, day
, hour
, minute
, second
,
millisecond
.
See: time/convert()
var d1 = new Date(2012, 4, 5);
var d2 = new Date(2013, 4, 8);
diff(d1, d2); // 31795200000
diff(d1, d2, 'hour'); // 8832
diff(d1, d2, 'week'); // 52.57142857142857
diff(d1, d2, 'month'); // 12.096774193548388
diff(d1, d2, 'year'); // 1.0082191780821919
Checks if it's a leap year according to the Gregorian calendar.
see: totalDaysInMonth()
isLeapYear(2012); // true
isLeapYear(2013); // false
isLeapYear(new Date(2012, 2, 28)); // true
Check if both dates are the "same".
You can pass an optional period used to set the comparisson precision.
Available periods: year
, month
, week
, day
, hour
, minute
, second
.
var date1 = new Date(2013, 1, 3);
var date2 = new Date(2013, 2, 9);
isSame(date1, date2); // false
isSame(date1, date2, 'day'); // false
isSame(date1, date2, 'month'); // false
isSame(date1, date2, 'year'); // true
Parses an ISO8601 date and returns the
number of milliseconds since January 1, 1970, 00:00:00 UTC, or NaN
if it is
not a valid ISO8601 date.
This parses all ISO8601 dates, including dates without times, ordinal dates, and the compact representation (omitting delimeters). The only exception is ISO week dates, which are not parsed.
If no time zone offset is specified, it assumes UTC time.
// Jan 01, 1970 00:00 GMT
parseIso('1970-01-01T00:00:00') // 0
parseIso('1970-001') // 0
parseIso('1970-01-01') // 0
parseIso('19700101T000000.00') // 0
parseIso('1970-01-01T02:00+02:00') // 0
// Jan 02, 2000 20:10 GMT+04:00
parseIso('2000-01-02T20:10+04:00') // 946829400000
Get a number between 1 to 4 that represents the quarter of the year.
quarter(new Date(2013, 1, 19)); // 1
quarter(new Date(2013, 4, 12)); // 2
quarter(new Date(2013, 7, 25)); // 3
quarter(new Date(2013, 10, 8)); // 4
Get a new Date at the start of the period.
Available periods: year
, month
, week
, day
, hour
, minute
, second
.
// Apr 05 2013 11:27:43
var date = new Date(2013, 3, 5, 11, 27, 43, 123);
startOf(date, 'year'); // Jan 01 2013 00:00:00
startOf(date, 'month'); // Apr 01 2013 00:00:00
startOf(date, 'day'); // Apr 05 2013 00:00:00
startOf(date, 'hour'); // Apr 05 2013 11:00:00
Format date based on strftime format.
Replaced tokens:
var date = new Date(2013, 3, 8, 9, 2, 4);
strftime(date, '%Y-%m-%d'); // "2013-04-08"
strftime(date, '%R'); // "09:02"
strftime(date, '%Y-%m-%dT%H:%M:%S%z'); // "2013-04-08T09:02:04+0000"
You can also set a custom locale:
var ptBr = require('mout/date/i18n/pt-BR');
strftime(date, '%a, %d %b', ptBr); // 'Seg, 08 Abr'
strftime(date, '%A, %d %B', ptBr); // 'Segunda, 08 Abril'
To set it globally:
require('mout/date/i18n_').set( customLocaleData );
See date/i18n for localization examples.
Return timezone abbreviation or similar data.
The result will vary based on the OS/browser since some environments doesn't provide enough info about the current locale.
// IE 7-8
timezoneAbbr(new Date()); // "-0500"
// Chrome, FF, V8
timezoneAbbr(new Date()); // "EST"
Return time zone as hour and minute offset from UTC (e.g. +0900).
It's important to note that JavaScript Date object will use the system locale info to determinate the timezone offset and that daylight saving time affects the result.
// if system locale is EST
timezoneOffset(new Date()); // -0500
Returns the amount of days in the month taking into consideration leap years (following Gregorian calendar).
see: isLeapYear()
totalDaysInMonth(2008, 1); // 29 (leap year)
totalDaysInMonth(2009, 1); // 28
// you can also pass a Date object as single argument
totalDaysInMonth( new Date(2013, 0, 1) ); // 31
Returns the amount of days in the year taking into consideration leap years (following Gregorian calendar).
see: isLeapYear()
, totalDaysInMonth()
totalDaysInYear(2008); // 366 (leap year)
totalDaysInYear(2009); // 365
// you can also pass a Date object as single argument
totalDaysInYear( new Date(2013, 0, 1) ); // 365
Returns how many weeks elapsed since start of the year (0..53
).
firstDayOfWeek
can be 0
(Sunday) or 1
(Monday). By default weeks start at
Sunday.
It will return 0
if date
is before the first firstDayOfWeek
of the year.
// Tue Jan 01 2013
weekOfTheYear( new Date(2013,0,1) ); // 0
// Wed Jan 09 2013
weekOfTheYear( new Date(2013,0,9) ); // 1
// Sun Jan 01 2012
weekOfTheYear( new Date(2012,0,1) ); // 1
// Mon Jan 09 2012
weekOfTheYear( new Date(2012,0,9) ); // 2
For more usage examples check specs inside /tests
folder. Unit tests are the
best documentation you can get...