C.J.
C.J.

Reputation: 6869

Difference between these dates in Javascript? And why do not all browsers give same result?

var dt = new Date("2012-04-23T12:00:00");

var dtz = new Date("2012-04-23T12:00:00Z");

If the Z is present I get a different time.

When the Z is present is it converting the Date to the browser's local time and when not present assuming it is already in local time?

I get different results in FF than Chrome. Chrome always gives me the same time. FF treats them as different. How should I be dealing with UTC dates from the server?

Upvotes: 0

Views: 556

Answers (3)

zumalifeguard
zumalifeguard

Reputation: 9016

According to ISO 8601, IF no UTC relation information is given with a time representation, the time is assumed to be in local time.

If can verify the correct behavior on both Firefox, Safari and Internet Explorer:

The following should return: false

new Date("2014-05-09T22:12:18.893Z").valueOf() === new Date("2014-05-09T22:12:18.893").valueOf()

If you try the same thing on Chrome or Opera, it will incorrect indicate: true

The moral of the story is, if you have a string in the above format, add a Z at the end.

Upvotes: 0

georg
georg

Reputation: 214959

"Z" is a military time zone corresponding to UT (aka UTC, aka GMT). So basically, 'nnn Z' means "how late is it in your time zone when it's 'nnn' in Greenwich". For example, I'm in CEST which is GMT+2 so this

new Date("2012-04-23T12:00:00Z")

returns for me:

Mon Apr 23 2012 14:00:00 GMT+0200 (CEST)

As to dates with a TZ specifier, they seem to be treated differently in Firefox (which assumes local TZ) and Chrome (which assumes UTC). For safety, I'd suggest always using an explicit TZ specifier.

Upvotes: 2

orif
orif

Reputation: 362

var dt = new Date("2012-04-23T12:00:00");

var dtz = new Date("2012-04-23T12:00:00Z");

tried it with alert() and got these messages

alert(dt);

 Mon Apr 23 2012 12:00:00 GMT+0500 (West Asia Standard Time)

alert(dtz);

Mon Apr 23 2012 17:00:00 GMT+0500 (West Asia Standard Time)

it means that if you create the date without "Z", it returns browsers's local time at GMT, mentioning your time zone is below or above GMT

and if you create it with "Z", it will show the local time at your time zone, referring to your time zone.

Upvotes: 1

Related Questions