Fork me on Github

underscore.date by timrwood

Parsing + Manipulation + Formatting + Timeago + I18n + NodeJS

And all without modifying the native Date prototype, so it's safe for kids!

 

What is this?

Underscore.date is a javascript date library that helps parse, manipulate, and format dates.

It doesn't modify the native `Date` prototype, so it's safe to drop into any site.

While not dependant on underscore, it does follow the same philosophy, and if underscore exists on your page, underscore.date will mix itself in.

Download : 2.02 kb (min + gzip)

You can download this project in either zip or tar formats.

You can also clone the project with Git by running:

$ git clone git://github.com/timrwood/underscore.date

 

Works in the browser and in Node.js!

Node.js

npm install underscore.date
var _date = require('underscore.date');
console.log(_date('September 9 1999').fromNow());

In the browser

If underscore exists, underscore.date will mix itself into the underscore namespace, so you can use as you would use an underscore function.

_.date('September 9 1999').fromNow();

Otherwise, you should use _date.

_date('September 9 1999').fromNow();

Examples

Format a Unix timestamp

_date(1300291340510).format("MMMM Do, YYYY") // >>> "March 16th, 2011" 

Get timeago for a date string

_date("7-10-1986", "MM-DD-YYYY").fromNow() // >>> "25 years ago" 

Get the current time and add 3 hours

_date().add({h:3}).fromNow() // >>> "in 3 hours" 

Change the language

_date(1300291340510).format("MMMM Do, YYYY") // >>> "March 16th, 2011"
_date.lang('pt');
_date(1300291340510).format("MMMM Do, YYYY") // >>> "Março 16o, 2011"
_date.lang('en');
_date(1300291340510).format("MMMM Do, YYYY") // >>> "March 16th, 2011"

Tests

underscore.date unit tests

underscore.date performance tests

Full source code and documentation at http://github.com/timrwood/underscore.date