Parse string to date with moment.js Parse string to date with moment.js javascript javascript

Parse string to date with moment.js


I always seem to find myself landing here only to realize that the title and question are not quite aligned.

If you want a moment date from a string:

const myMomentObject = moment(str, 'YYYY-MM-DD')

From moment documentation:

Instead of modifying the native Date.prototype, Moment.js creates a wrapper for the Date object.

If you instead want a javascript Date object from a string:

const myDate = moment(str, 'YYYY-MM-DD').toDate();


You need to use the .format() function.

MM - Month number

MMM - Month word

var date = moment("2014-02-27T10:00:00").format('DD-MM-YYYY');var dateMonthAsWord = moment("2014-02-27T10:00:00").format('DD-MMM-YYYY');

FIDDLE


No need for moment.js to parse the input since its format is the standard one :

var date = new Date('2014-02-27T10:00:00');var formatted = moment(date).format('D MMMM YYYY');

http://es5.github.io/#x15.9.1.15