In PHP if i have:
$date = "2012-01-18 16:00";
I can do:
$newDate = new DateTime($date);
In JS (jQuery) i have:
var date = "2012-01-18 16:00";
why i can't:
var newDate = new Date(date);
? This return me Invalid date.
In order for that string to be parsed, you need a T between the date and the time
var date = "2012-01-18T16:00";
var newDate = new Date(date);
so to fix your original code:
var date = "2012-01-18 16:00";
date = date.replace(" ", "T");
var newDate = new Date(date);
The Date constructor will also take a year, month, and day
var newDate = new Date(2012, 1, 18)
though to get that to work you'd have to split your string up.
You (almost) can:
new Date(year, month, day [, hour, minute, second, millisecond ]);
Even better, include datejs and do this:
Date.parse("2012-01-18 16:00");
Datejs is an open-source Javascript Date library that is able to recognize many date input formats, including the one that you supplied in your question. It also works within the local time zone. If you don't want to use that library, you can still simply replace the space in your date string with a T, as such:
var date = "2012-01-18 16:00";
var newDate = new Date(date.replace(" ","T"));
Note that this method assumes that your input is in GMT and will subtract or add the appropriate number of hours for the local user, whereas Datejs won't.