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);
parse
is exactly whatnew Date
uses internally, so don't expect one to work if they other doesn't). There is really no reason for any external API to not return plain vanilla standard date formats. If at all possible, try to get the API changed. Failing that, use a date-parsing library such as moment. The last thing you want to do is spend the rest of your life chasing down some bug in some regexp you wrote to fudge date formats.