Javascript, Date.parse with no timezone

I am having a problem with Javascript dates.

I receive an JSON that contains a date, when I try to get the date object it returns the value in a different timezone and usually move the date to a day before at 20hrs.

Example:

The value in json is: "2014-06-01T00:00:00"

When I do

var d2 = new Date(Date.parse("2014-06-01T00:00:00"))

it returns

Sat May 31 2014 20:00:00 GMT-0400 (Eastern Daylight Time)

When I expected

Sun Jun 01 2014 00:00:00 GMT-0400

how can I fix this issue?

thank you

Jon Skeet
people
quotationmark

From the Mozilla documentation of Date.parse:

ECMAScript 5 ISO-8601 format support

Alternatively, the date/time string may be in ISO 8601 format. For example, "2011-10-10" (just date) or "2011-10-10T14:48:00" (date and time) can be passed and parsed. The UTC time-zone is used to interpret arguments in ISO 8601 format that do not contain time zone information.

Your value doesn't include time zone information, so it's assumed to be in UTC. Midnight UTC on the day in question is 8pm in your local time zone. Note that a Date object doesn't have the concept of a time zone in itself - it's just a number of milliseconds since the Unix epoch. When you convert it to a string with toString, that uses the local time zone of the browser. You can use toUTCString to convert to a text representation using UTC instead (so in this case, you'd end up with the UTC midnight you started from).

Now in terms of what you can do to "fix" the issue... you need to start off by understanding what value you're trying to represent, and what you want to do with it. We can't really help you with that without more information.

people

See more on this question at Stackoverflow