Parse, validate, manipulate, and display dates in javascript.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


Author: Tim Wood

Version: 0.4.0 is a JavaScript Date utility library built on top of Underscore.js

It adds utility functions for working with Date objects without extending Date.prototype.

In addition to the date creation and manipulation functions, there are a few functions for displaying a date in human readable formats.

var someday = new Date(2010, 1, 14, 15, 25, 50, 125);"dddd, MMMM Do YYYY, h:mm:ss a"); // "Sunday, February 14th 2010, 3:25:50 pm"; // "20 days ago"

Filesize : 1.7 kb minified + gzipped

minified gzipped
3.81 kb 1.7 kb


npm install


var _date = require('');
console.log('September 9 1999').fromNow());

Underscore mixin

If underscore exists, _date will mix itself into the underscore namespace, so you can do this:'September 9 1999').fromNow()

Otherwise, you should use _date.

The library works by creating a date wrapper object using

To create that wrapper, you can pass any of the following data types in.

Date Date(2010, 1, 14, 15, 25, 50, 125))

Any valid Date object. For more information on Date objects, see the JavaScript Date documentation at MDN

Array[2010, 1, 14, 15, 25, 50, 125])

The array should mirror the parameters passed into Date.UTC().

[year, month, date, hours, minutes, seconds, milliseconds]

Any value past the year is optional, and will default to the lowest possible number.

String"Dec 25, 1995")

A string that can be parsed by Date.parse().


An integer value representing the number of milliseconds since 1 January 1970 00:00:00 UTC.


If no value is passed to a 'dateInput' parameter, it will default to the current time using new Date(). === Date())

Returns the current date.

Pass true to return a UNIX timestamp, otherwise it is just a shortcut to Date()).



Returns true if the year is a leap year, false if it is not

Pass the year number to this function. To check for a leap year on a object, use

Examples :

_date.isLeapYear(2000) // true
_date.isLeapYear(2001) // false
_date.isLeapYear(2100) // false Functions

Adds time per the object passed in.

The object should have key value pairs as shown below.

    ms:200, // milliseconds
    s:10, // seconds
    m:10, // minutes (note: lowercase)
    h:2, // hours
    d:3, // days
    M:2, // months (note: uppercase)
    y:3 // years

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:[2010, 0, 31]) // January 31[2010, 0, 31]).add({M:1}) // February 28

Functions the same as, only using subtraction instead of addition.

Example:[2010, 1, 28]) // February 28[2010, 1, 28]).subtract({M:1}) // January 28

Returns a human readable string based on the format string that was passed in. Date(2010, 1, 14, 15, 25, 50, 125)).format("dddd, MMMM Do YYYY, h:mm:ss a"); // "Sunday, February 14th 2010, 3:25:50 pm" Date(2010, 1, 14, 15, 25, 50, 125)).format("ddd, hA"); // "Sun, 3PM"

The formats are created by creating a string of replacable characters.


Input Output
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
YY 70 71 ... 29 30
YYYY 1970 1971 ... 2029 2030
a am pm
H 0 1 ... 22 23
HH 00 01 ... 22 23
h 1 2 ... 11 12
hh 01 02 ... 11 12
m 0 1 ... 58 59
mm 00 01 ... 58 59
s 0 1 ... 58 59
ss 00 01 ... 58 59
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'.

Firefox, Chrome, Safari, etc. == 'Eastern Standard Time'
Internet Explorer, etc. == 'EST', withoutSuffix:boolean, asMilliseconds:boolean)

Returns a string as relative time ('minutes ago', '5 months ago', etc).

You can pass anything that you would pass to as the first parameter, or a object.[2007, 0, 29]).from([2007, 0, 28])) // "a day ago");

You can pass true as the second parameter to return without the prefixes and suffixes.[2007, 0, 29]).from([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.[2007, 0, 29]).from([2007, 0, 28]), true , true) // -86400000);[2007, 0, 27]).from([2007, 0, 28]), true , true) // 86400000);

The base strings for this function can be customized with, asMilliseconds:boolean)

A shortcut for, withoutSuffix:boolean, asMilliseconds:boolean)

Returns true if the year is a leap year, false if it is not

Localization and Customization

To customize the wording of and you can use the function, passing in an object with the parameters you wish to overwrite.{
    weekdays:["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"],
    weekdaysShort:["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"],
    months:["January", "February", "March", "April", "May", "June", "July", 
        "August", "September", "October", "November", "December"],
    monthsShort:["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
        return (Math.floor(number / 10) === 1) ? 'th' :
			(number % 10 === 1) ? 'st' :
			(number % 10 === 2) ? 'nd' :
			(number % 10 === 3) ? 'rd' : 'th';
	timeago : {
		future: "in %s",
		past: "%s ago",
        ss: "less than a minute",
        m: "about a minute",
        mm: "%d minutes",
        h: "about an hour",
        hh: "about %d hours",
        d: "a day",
        dd: "%d days",
        M: "about a month",
        MM: "%d months",
        y: "about a year",
        yy: "%d years"


["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]

An array of day names, starting with Sunday.


["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]

An array of abbreviated day names, starting with Sunday.


["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]

An array of the names of the months, starting with January.


["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]

An array of the abbreviated names of the months, starting with January.


    return (Math.floor(number / 10) === 1) ? 'th' :
		(number % 10 === 1) ? 'st' :
		(number % 10 === 2) ? 'nd' :
		(number % 10 === 3) ? 'rd' : 'th';

A function that returns a string to be appended to the number passed in. More information on ordinal numbers


    future: "in %s",
    past: "%s ago",
    ss: "less than a minute",
    m: "about a minute", // note: lowercase
    mm: "%d minutes", // note: lowercase
    h: "about an hour",
    hh: "about %d hours",
    d: "a day",
    dd: "%d days",
    M: "about a month", // note: uppercase
    MM: "%d months", // note: uppercase
    y: "about a year",
    yy: "%d years"

The strings used in

future and past are used as the suffixes/prefixes.

For all these values, a single character refers to the singular, and an double character refers to the plural.


There are a bunch of tests in the test/ folder. Check them out. If you think some tests are missing, open an issue, and I'll add it.

Speed tests

Floor vs bitwiseor vs bitwisenor vs parseint

Switch/case vs object of functions lookup

Left zero filling

Thanks to...

The folks over at date.js.

Everyone who helped with php.js date.

Ryan McGeary for his work on the jQuery timeago plugin.



Added to npm. Removed dependancies on underscore.


Added 'z' and 'zz' to Cleaned up some redundant code to trim off some bytes.


Cleaned up the namespace. Moved all date manipulation and display functions to the object.


Switched to the Underscore methodology of not mucking with the native objects' prototypes. Made chaining possible.


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.


Changed function names to be more concise. Changed date format from php date format to custom format.


Initial release