Jeremy B. Jeremy B. - 1 year ago 84
Javascript Question

Convert datetime to valid JavaScript date

I have a datetime string being provided to me in the following format:

yyyy-MM-dd HH:mm:ss
2011-07-14 11:23:00

When attempting to parse it into a JavaScript
object it fails. What is the best way to convert this into a format that JavaScript can understand?

The answers below suggest something like

var myDate = new Date('2011-07-14 11:23:00');

Which is what I was using. It appears this may be a browser issue. I've made a for this. It works OK for me in Chrome. In Firefox 5.0.1 on OS X it returns
Invalid Date

Answer Source

This works everywhere including Safari 5 and Firefox 5 on OS X.


var date_test = new Date("2011-07-14 11:23:00".replace(/-/g,"/"));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download