momment js code example

Example 1: moment format date

moment().format();                                // "2019-08-12T17:52:17-05:00" (ISO 8601, no fractional seconds)
moment().format("dddd, MMMM Do YYYY, h:mm:ss a"); // "Monday, August 12th 2019, 5:52:00 pm"
moment().format("ddd, hA");                       // "Mon, 5PM"

Example 2: momentjs

npm install moment --save   # npm
yarn add moment             # Yarn
moment.locale();         // en
moment().format('LT');   // 8:49 AM
moment().format('LTS');  // 8:49:26 AM
moment().format('L');    // 10/13/2020
moment().format('l');    // 10/13/2020
moment().format('LL');   // October 13, 2020
moment().format('ll');   // Oct 13, 2020
moment().format('LLL');  // October 13, 2020 8:49 AM
moment().format('lll');  // Oct 13, 2020 8:49 AM
moment().format('LLLL'); // Tuesday, October 13, 2020 8:49 AM
moment().format('llll'); // Tue, Oct 13, 2020 8:49 AM

Example 3: javascript moment

m = moment('2013-03-01', 'YYYY-MM-DD')

Example 4: moment

moment("20111031", "YYYYMMDD").fromNow(); // há 9 anos
moment("20120620", "YYYYMMDD").fromNow(); // há 8 anos
moment().startOf('day').fromNow();        // há 13 horas
moment().endOf('day').fromNow();          // em 11 horas
moment().startOf('hour').fromNow();       // há 10 minutos

Example 5: moment js

moment().subtract(Number, String);
moment().subtract(Duration);
moment().subtract(Object);

Example 6: momentjs docs

npm install moment