How to Parse string to date with moment.js

By FoxLearn 2/16/2024 9:09:21 AM   74
This post shows you how to convert string to date using moment.js

If you want to parse the following string with moment.js '2020-02-27T10:00:00' and output day month year (27-Feb-2020) you can modify your code as shown below.

var date1 = moment("2020-02-27T10:00:00").format('DD-MM-YYYY');
var date2 = moment("2020-02-27T10:00:00").format('DD-MMM-YYYY');
alert(date1);
alert(date2);

or if you want to display 2020-02-27 you can modify

var date1 = moment("27/02/2020", 'DD/MM/YYYY').format('YYYY-MM-DD');

and don't forget to add moment.js to your html file.

You can use the format() function to display format your datetime.

MM - Month number
MMM - Month word

 

Related