0

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 ?

2 Answers 2

2

try new

alert(new Date(Date.parse("2014-02-24")))
Sign up to request clarification or add additional context in comments.

3 Comments

Wow, it was that simple? Thanks. If I was only doing the Date.parse should I put new in front of it as well?
No. Date.parse is correct. Any constructor should use new.
Actually, you can simplify it down to console.log(new Date("2014-02-24T09:49:22.000-0800")). new Date can also parse for itself.
0

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());

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.