Jeremy B. Jeremy B. - 28 days ago 4
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


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,"/"));