moment duration format code example

Example 1: 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 2: create moment duration

moment.duration(2, 'seconds');
moment.duration(2, 'minutes');
moment.duration(2, 'hours');
moment.duration(2, 'days');
moment.duration(2, 'weeks');
moment.duration(2, 'months');
moment.duration(2, 'years');
moment.duration('2', 'years'); // from 2.25.0

// or create from an object
moment.duration({
    seconds: 2,
    minutes: 2,
    hours: 2,
    days: 2,
    weeks: 2,
    months: '2',
    years: '2'
});

// or create from an iso 8601 string
moment.duration('P1Y2M3DT4H5M6S');
moment.duration('P1M');

// duration format strings with a space between days and rest is also supported.
moment.duration('7 23:59:59.999');

Example 3: momentDurationFormatSetup

var moment = require("moment-timezone");
var momentDurationFormatSetup = require("moment-duration-format");

momentDurationFormatSetup(moment);
typeof moment.duration.fn.format === "function";
// true
typeof moment.duration.format === "function";
// true