Menu

Moment.js

January 10, 2018 - javascript, Node.js

Parse, validate, manipulate, and display dates and times in JavaScript.

Moment.js docs

moment().format('MMMM Do YYYY, h:mm:ss a'); // January 25th 2018, 11:19:44 am
moment().format('dddd');                    // Thursday
moment().format("MMM Do YY");               // Jan 25th 18
moment().format('YYYY [escaped] YYYY');     // 2018 escaped 2018
moment().format();                          // 2018-01-25T11:19:44+01:00

moment("20111031", "YYYYMMDD").fromNow(); // 6 years ago
moment("20120620", "YYYYMMDD").fromNow(); // 6 years ago
moment().startOf('day').fromNow();        // 11 hours ago
moment().endOf('day').fromNow();          // in 13 hours
moment().startOf('hour').fromNow();       // 20 minutes ago

Parse date

const moment = require('moment');

var date = '22.05.2001';
console.log(date); // 22.05.2001
date = moment(date, "DD.MM.YYYY");
console.log(date); // moment("2001-05-22T00:00:00.000");

article_date = moment( '22.6.2014'+ ' ' +'22:00', 'DD.MM.YYYY, HH:mm' );
console.log(article_date); // moment("2014-06-22T22:00:00.000")