Date Time Javascript Libraries

Formatting Date Time With Javascript Libraries


Moment JS



Moment JS

Moment.js is a Date Time Javascript Library that can parse, validate, manipulate, and display dates and times in JavaScript. To download Moment.js, please visit the Moment.js website for the latest version.

Installation

To add Moment.js to your site, either download the latest version and upload it to your site’s Javascript directory or reference a copy hosted by a CDN as follows:

Your sites Javascript directory:

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

CDN:

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>

Examples

Format Dates in Moment JS Example

moment().format('MMMM Do YYYY, h:mm:ss a'); // January 24th 2018, 9:12:33 pm
moment().format('dddd');                    // Saturday
moment().format("MMM Do YY");               // Jan 24th 18
moment().format('YYYY [escaped] YYYY');     // 2018 escaped 2018
moment().format();                          // 2018-01-24T21:12:33-05:00

Relative Time in Moment JS Example

moment("20111031", "YYYYMMDD").fromNow(); // 7 years ago
moment("20120620", "YYYYMMDD").fromNow(); // 6 years ago
moment().startOf('day').fromNow();        // 21 hours ago
moment().endOf('day').fromNow();          // in 3 hours
moment().startOf('hour').fromNow();       // 13 minutes ago

Calendar Time in Moment JS Example

moment().subtract(10, 'days').calendar(); // 01/14/2018
moment().subtract(6, 'days').calendar();  // Last Sunday at 9:12 PM
moment().subtract(3, 'days').calendar();  // Last Wednesday at 9:12 PM
moment().subtract(1, 'days').calendar();  // Yesterday at 9:12 PM
moment().calendar();                      // Today at 9:12 PM
moment().add(1, 'days').calendar();       // Tomorrow at 9:12 PM
moment().add(3, 'days').calendar();       // Tuesday at 9:12 PM
moment().add(10, 'days').calendar();      // 12/04/2018

Multiple Locale Support in Moment JS Example

moment.locale();         // en
moment().format('LT');   // 9:12 PM
moment().format('LTS');  // 9:12:33 PM
moment().format('L');    // 01/24/2018
moment().format('l');    // 01/24/2018
moment().format('LL');   // January 24, 2018
moment().format('ll');   // Jan 24, 2018
moment().format('LLL');  // January 24, 2018 9:12 PM
moment().format('lll');  // Jan 24, 2018 9:12 PM
moment().format('LLLL'); // Saturday, January 24, 2018 9:12 PM
moment().format('llll');