codeBarer
codeBarer

Reputation: 2388

Javascript date parse error

I'm trying to convert string into epoch time in milliseconds using specs found on:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse

For some reason the following snippet of codes returns March 13 instead of Feb 24, 2014.

Snippet 1:

alert(Date(Date.parse("2014-02-24T09:49:22.000-0800")));

Output: Thu Mar 13 2014 21:51:41 GMT-0700 (Pacific Daylight Time)

Snippet 2:

alert(Date(Date.parse("2014-02-24")));

Output: Thu Mar 13 2014 21:51:41 GMT-0700 (Pacific Daylight Time)

Is this some sort of timezone issue or what is the mistake that i have done ?

Upvotes: 0

Views: 2756

Answers (2)

Mr.G
Mr.G

Reputation: 3559

Try this:

function parseDate(input) {
  var parts = input.split('-');
  return new Date(parts[0], parts[1]-1, parts[2]); // Note: months are 0-based
}

or

console.log(new Date(Date.parse("2014-02-08T00:00:00Z")).toString());

Upvotes: 0

bluetoft
bluetoft

Reputation: 5443

try new

alert(new Date(Date.parse("2014-02-24")))

Upvotes: 2

Related Questions