Tasos
Tasos

Reputation: 7577

Invalid Date in Javascript Date

I use an external API to get some data. Then I do some calculations in Javascript with those data. One field is date in this format: 2015-01-26 18:28:14

Then I have to parse this date. I tried with:

var last = "2015-01-26 18:28:14"
var login = new Date(last).getTime();

But I have an error of Invalid Date. I also tried:

var last = "2015-01-26 18:28:14"
var login = Date.parse(last);

Upvotes: 0

Views: 117

Answers (2)

atmd
atmd

Reputation: 7490

Running your code caused errors for me in firefox too

formatting the date like this resolved the issue

"2015/01/26 00:00:00"


var last = "2015/01/26 18:28:14"
var login = new Date(last).getTime();

if you date is coming back with the '-' you can simply do a replace

var d = "2015-01-26 18:28:14";
var login = new Date(d.replace('-', '/')).getTime();

Upvotes: 0

anddoutoi
anddoutoi

Reputation: 10111

You could try insert the character T between the date and the time.

ECMAScript 5 adds support for ISO-8601 dates and times. ISO-8601 stipulates that timestamps with both date and time should be written 2015-01-26T18:28:14.

Note that parse returns:

the number of milliseconds since January 1, 1970, 00:00:00 UTC

See Date.parse() for more info.

Upvotes: 1

Related Questions