Jeremy B. Jeremy B. - 2 months ago 8
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
date()
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 http://jsfiddle.net/czeBu/ for this. It works OK for me in Chrome. In Firefox 5.0.1 on OS X it returns
Invalid Date
.

Answer

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

DEMO HERE

var date_test = new Date("2011-07-14 11:23:00".replace(/-/g,"/"));