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');
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');