js moment format code example
Example 1: npm install moment
npm i moment --save
Example 2: moment format
moment().format('MMMM Do YYYY, h:mm:ss a'); // September 30th 2020, 2:48:17 pm
moment().format('dddd'); // Wednesday
moment().format("MMM Do YY"); // Sep 30th 20
moment().format('YYYY [escaped] YYYY'); // 2020 escaped 2020
moment().format(); // 2020-09-30T14:48:17+02:00
Example 3: moment.js
npm install moment --save
const moment = require('moment'); //require
moment().format('MMMM Do YYYY, h:mm:ss a'); // February 13th 2021, 1:49:03 am
moment().format('dddd'); // Saturday
moment().format("MMM Do YY"); // Feb 13th 21
moment().format('YYYY [escaped] YYYY'); // 2021 escaped 2021
moment().format(); // 2021-02-13T01:49:29+01:00
moment("20111031", "YYYYMMDD").fromNow(); // 9 years ago
moment("20120620", "YYYYMMDD").fromNow(); // 9 years ago
moment().startOf('day').fromNow(); // 2 hours ago
moment().endOf('day').fromNow(); // in a day
moment().startOf('hour').fromNow(); // an hour ago
moment().subtract(10, 'days').calendar(); // 02/03/2021
moment().subtract(6, 'days').calendar(); // Last Sunday at 1:50 AM
moment().subtract(3, 'days').calendar(); // Last Wednesday at 1:50 AM
moment().subtract(1, 'days').calendar(); // Yesterday at 1:50 AM
moment().calendar(); // Today at 1:50 AM
moment().add(1, 'days').calendar(); // Tomorrow at 1:50 AM
moment().add(3, 'days').calendar(); // Tuesday at 1:50 AM
moment().add(10, 'days').calendar(); // 02/23/2021
moment.locale(); // en
moment().format('LT'); // 1:50 AM
moment().format('LTS'); // 1:50:49 AM
moment().format('L'); // 02/13/2021
moment().format('l'); // 2/13/2021
moment().format('LL'); // February 13, 2021
moment().format('ll'); // Feb 13, 2021
moment().format('LLL'); // February 13, 2021 1:50 AM
moment().format('lll'); // Feb 13, 2021 1:50 AM
moment().format('LLLL'); // Saturday, February 13, 2021 1:50 AM
moment().format('llll'); // Sat, Feb 13, 2021 1:51 AM
Example 4: npm momen
npm i moment
Example 5: moment add seconds
var travelTime = moment().add(642, 'seconds').format('hh:mm A');// it will add 642 seconds in the current time and will give time in 03:35 PM format
var travelTime = moment().add(11, 'minutes').format('hh:mm A');// it will add 11 mins in the current time and will give time in 03:35 PM format; can use m or minutes
var travelTime = moment().add(2, 'hours').format('hh:mm A');// it will add 2 hours in the current time and will give time in 03:35 PM format
Example 6: format a date moment
moment(testDate).format('MM/DD/YYYY');