JavaScript Libs: Moment.js – Parse, manipulate and display dates/times

Category: Javascript Libs Tags:
May 22, 2018
JavaScript Libs

JavaScript Libs is a series of articles introducing useful JavaScript libraries to developers

Moment.js

Use Moment.js to parse, validate, manipulate, and display dates and times in JavaScript

What it does

Moment.js is currently the de facto library for handling and manipulating dates and times. Comprehensive and probably used in all major projects requiring datetime manipulation. Supports dozens of local time formats. Works in browser and with Node.js.

License: Moment.js is free and licensed under MIT.
Popularity: Massive. npm has more than 4 million downloads per week.

Features

  • Works in virtually all JavaScript environments
  • Parse dates and times
  • Get/Set dates and times
  • Manipulate dates and times
  • Display dates and times
  • Query dates and times
  • Durations
  • Includes other date and time Utilities
  • Locale support

Where to Find Moment:

On npm: https://www.npmjs.com/package/moment
On githubhttps://github.com/moment/moment

Recommended Installation:

[npm] – npm install moment –save
[yarn] – yarn add moment
[Nuget] – Install-Package Moment.js

Examples:

var now = moment(); // get todays date/time
var day = moment("2018-12-25"); // get Christmas day 2018
moment().startOf('day').fromNow(); // relative time, e.g. 5 hours ago
moment().add(1, 'days').calendar(); // tomorrow. add or subtract days/weeks/months
moment().format("dddd, MMMM Do YYYY, h:mm:ss a"); // format date, "Monday, May 23th 2018, 6:25:50 pm"
moment('2017-09-20').isBefore('2018-01-01', 'year'); // compare - true

Further Examples:

http://momentjs.com/docs/