new Date moment format code example

Example 1: moment format date

moment.locale();         // en
moment().format('LT');   // 12:25 PM
moment().format('LTS');  // 12:25:27 PM
moment().format('L');    // 03/26/2021
moment().format('l');    // 3/26/2021
moment().format('LL');   // March 26, 2021
moment().format('ll');   // Mar 26, 2021
moment().format('LLL');  // March 26, 2021 12:25 PM
moment().format('lll');  // Mar 26, 2021 12:25 PM
moment().format('LLLL'); // Friday, March 26, 2021 12:25 PM
moment().format('llll'); // Fri, Mar 26, 2021 12:25 PM

Example 2: date format in moment js

moment().format('MMMM Do YYYY, h:mm:ss a'); // April 14th 2021, 5:38:08 pm
moment().format('dddd');                    // Wednesday
moment().format("MMM Do YY");               // Apr 14th 21
moment().format('YYYY [escaped] YYYY');     // 2021 escaped 2021
moment().format();                          // 2021-04-14T17:38:08+06:00

Example 3: moment format date

moment().subtract(10, 'days').calendar(); // 03/16/2021
moment().subtract(6, 'days').calendar();  // Last Saturday at 12:25 PM
moment().subtract(3, 'days').calendar();  // Last Tuesday at 12:25 PM
moment().subtract(1, 'days').calendar();  // Yesterday at 12:25 PM
moment().calendar();                      // Today at 12:25 PM
moment().add(1, 'days').calendar();       // Tomorrow at 12:25 PM
moment().add(3, 'days').calendar();       // Monday at 12:25 PM
moment().add(10, 'days').calendar();      // 04/05/2021

Example 4: moment format date

moment("20111031", "YYYYMMDD").fromNow(); // 9 years ago
moment("20120620", "YYYYMMDD").fromNow(); // 9 years ago
moment().startOf('day').fromNow();        // 12 hours ago
moment().endOf('day').fromNow();          // in 12 hours
moment().startOf('hour').fromNow();       // 26 minutes ago

Tags:

Misc Example