Parse 'Date & Time' string in Javascript which are of custom format

yuva picture yuva · Jan 17, 2015 · Viewed 72.5k times · Source

I have to parse a date and time string of format "2015-01-16 22:15:00". I want to parse this into JavaScript Date Object. Any help on this?

I tried some jquery plugins, moment.js, date.js, xdate.js. Still no luck.

Answer

moises.vilar picture moises.vilar · Jan 17, 2015

With moment.js you can create a moment object using the String+Format constructor:

var momentDate = moment('2015-01-16 22:15:00', 'YYYY-MM-DD HH:mm:ss');

Then, you can convert it to JavaScript Date Object using toDate() method:

var jsDate = momentDate.toDate();