Work with date and time in JavaScript? Try moment.js!

More advanced operations on dates are not so easy, so glad to have solutions like moment.js!

Date and time in JavaScript with moment.js library

When basic operations and dates and time are quite simple, more advanced like parsing or calculating – aren’t. It’s good to use one of available solutions, such as moment.js! We really like it and often use in various web projects.

You can easy parse, validate, manipulate and display dates in JavaScript.

Working with date and time in JavaScript - moment.js
Working with date and time in JavaScript – moment.js

Usage

Download the library (we can also get locales) or use packages, for example:

bower install moment –save

npm install moment –save

Sample operations:

moment().format('MMMM Do YYYY, h:mm:ss a'); // Full date and time
moment().format('dddd');                    // Day of week
moment().format("MMM Do YY");               // Date, e.g. May 14th 16
moment().format('YYYY [escaped] YYYY');     // Year - escaped 
moment().format();
// => this will simply return e.g. 2016-05-14T15:38:27+02:00

Relative Time:

moment("20111031", "YYYYMMDD").fromNow(); // 5 years ago
moment().startOf('day').fromNow();        // 16 hours ago
moment().startOf('hour').fromNow();       // 38 minutes ago
// etc ...

Calendar Time:

moment().subtract(10, 'days').calendar(); // 05/04/2016
moment().subtract(6, 'days').calendar();  // Last Sunday at 3:38 PM
moment().calendar();                      // Today at 3:38 PM
moment().add(1, 'days').calendar();       // Tomorrow at 3:38 PM
moment().add(10, 'days').calendar();      // 05/24/2016

It really help us to work with dates in JavaScript!

License: MIT

Project homepage:

http://momentjs.com

GitHub:

https://github.com/moment/moment

See also:

Material time picker written in JavaScript

Analog and digital jQuery world clock plugin – jClocksGMT.js

DrumJS – A jQuery animated date picker and value selector (iOS style)

How to Make a stylish Digital Clock with jQuery and CSS3

Related topics:

http://tutorialzine.com/2012/08/quick-tip-handle-date-and-time-like-a-boss-with-moment-js

http://stackoverflow.com/questions/10967736/how-to-use-moment-js

“Date and time in JavaScript”

3! 2! 1!

Share:Share on Facebook4Share on Google+1Tweet about this on TwitterShare on LinkedIn1Pin on Pinterest1