moment typescript code example

Example 1: format a date moment

moment(testDate).format('MM/DD/YYYY');

Example 2: moment js get french time 20:00:00

moment().format();                                // "2014-09-08T08:02:17-05:00" (ISO 8601, no fractional seconds)
moment().format("dddd, MMMM Do YYYY, h:mm:ss a"); // "Sunday, February 14th 2010, 3:25:50 pm"
moment().format("ddd, hA");                       // "Sun, 3PM"
moment('gibberish').format('YYYY MM DD');         // "Invalid date"

Example 3: moment format heure

var time = "15:30:00";
var formatted = moment(time, "HH:mm:ss").format("LT");
console.log(formatted); 
//it will return 3:30 PM

Example 4: moment get day

var check = moment(n.entry.date_entered, 'YYYY/MM/DD');

var month = check.format('M');
var day   = check.format('D');
var year  = check.format('YYYY');

Example 5: 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 6: momentjs docs

npm install moment

Tags:

Misc Example