Flexible date manipulation in javascript

Date manipulation is a common programming task a developer does regularly and many times can get tricky and cumbersome. If youy web applications relies heavily on date manipulation on the frontend than moment.js is the library you have been looking for.

In short this is what you can do with moment.js

moment("20111031", "YYYYMMDD").fromNow(); // 2 years ago
moment().endOf('day').fromNow(); // in 2 hours
moment().startOf('hour').fromNow(); // 9 minutes ago
moment().subtract('days', 6).calendar(); // Last Wednesday at 10:09 PM
moment().add('days', 1).calendar(); // Tomorrow at 10:10 PM

moment.js is a javascript library that you can use in a browser or with node.js.

The simplest workable script is the following which will return a date string – ‘2014-04-01T22:15:36+05:30′. The ‘moment’ object is the current moment in time.

<script src="moment.js"></script>

Passing a different format will return a different string.

moment().format("ddd, hA"); // "Tue, 10PM"

Date Manipulation

One of the tedious task in programming and which Moment.js makes it easier is date arithmetic.

var now = moment(); // Tue Apr 01 2014 21:28:31 GMT+0530
var later = moment().add('days', 2); // Thu Apr 03 2014 21:29:05 GMT+0530
// Shorthand for the above
var later = moment().add('d', 2);

To print variables as string use the toString method of the moment object.


You can also chain different methods together to arrive at a different date.

var later = moment().add('days', 2).add('months', 2); 
// Tue Jun 03 2014 21:28:31 GMT+0530

Like adding dates you can also subtract dates.

moment().subtract('days', 7);

Getting relative time

One of the frequently used time processing in web applications is of displaying the period between two dates. The ‘fromNow’ method takes care of that. Check the following code examples. This can be specially useful for displaying comment or article/post dates.

moment([2001, 0, 29]).fromNow(); // '13 years ago'
moment().subtract('days', 2).fromNow(); // '2 days ago'
moment().subtract('minutes', 2).fromNow(); // '2 minutes ago'

Of course this also works for future times.

moment([2015, 0, 29]).fromNow(); // 'in 10 months'
moment().add('days', 3).fromNow(); // 'in 3 days'
moment().add('minutes', 2).fromNow(); // 'in 2 minutes'

Customization of units

Moment.js is very easy to customize and various features of the library can be modified to suit your particular application. You can customize month, weekdays names and also write custom functions to create your own format.

Internationalization support

Moment.js has robust support for internationalization. You can load multiple languages and easily switch between them. In addition to assigning a global language, you can assign a language to a specific moment. You will need to include language specific files first.

moment(1316116057189).fromNow() // il y a une heure
moment(1316116057189).fromNow() // an hour ago

‘Moment.js’ is a comprehensive date manipulation library with a extensive documentation. We have only covered a fraction of what it offers and you should go over the official documentation for more details