Given the following code:
var date = new Date("2014-02-26T15:52:30");
date.getUTCHours();
// Outputs
// Chrome: 15
// Firefox: 18
// IE: 18
I'm not sure which date method I should use. Date.getHours
returns the correct hour in FF and IE, but incorrect in Chrome. And Date.getUTCHours()
is showing me the correct date, but is not ok in both IE and FF. What are the differences anyway? What UTC date is supposed to be?
I wonder if is there a CrossBrowser native solution... I wouldn't like to use a library for such a simple thing.
Given the following code:
var date = new Date("2014-02-26T15:52:30");
date.getUTCHours();
// Outputs
// Chrome: 15
// Firefox: 18
// IE: 18
I'm not sure which date method I should use. Date.getHours
returns the correct hour in FF and IE, but incorrect in Chrome. And Date.getUTCHours()
is showing me the correct date, but is not ok in both IE and FF. What are the differences anyway? What UTC date is supposed to be?
I wonder if is there a CrossBrowser native solution... I wouldn't like to use a library for such a simple thing.
Share asked Feb 26, 2014 at 19:41 darksoulsongdarksoulsong 15.4k14 gold badges51 silver badges96 bronze badges 5- Maybe you should specify the time zone in your Date string? Not sure how it's supposed to be interpreted otherwise according to the standard. – aknuds1 Commented Feb 26, 2014 at 19:46
- @aknuds1 And how can I specify the timezone? Is this part of the Date spec? Well, I cannot change the date string anyway, because this is the format I receive from the server. – darksoulsong Commented Feb 26, 2014 at 20:01
-
If you expect the timezone from the server to be -3 hours, you should append -03:00 to your date string, e.g.
new Date('2014-02-26T15:52:30-03:00').getUTCHours()
. – aknuds1 Commented Feb 26, 2014 at 20:06 - possible duplicate of Chrome interprets ISO time without Z as UTC; C# issue – Praveen Commented Feb 26, 2014 at 20:13
- 1 @Praveen Thanks, that confirms that Chrome is getting it right (yay Chrome). – aknuds1 Commented Feb 26, 2014 at 20:13
2 Answers
Reset to default 6Chrome interpret your date var date = new Date("2014-02-26T15:52:30");
the same as var date = new Date("2014-02-26T15:52:30Z");
(notice the z, indicating the utc timezone).
FF and IE interpret var date = new Date("2014-02-26T15:52:30");
as being in the timezone of the current user, in my case, it would be var date = new Date("2014-02-26T15:52:30-05:00");
(GMT-0500)
I strongly suggest that you stick with UTC for you puting need and perform the timezone transformation when you want to show the date to the user. Timezone can bee a real pain.
The problem is not with getUTCHours()
is with your dateString(2014-02-26T15:52:30
).
As per ISO8601, it is better to have either
var date = new Date("2014-02-26T15:52:30Z");
or
var date = new Date("2014-02-26T15:52:30+00:00");
This ensures mon among all browsers.
In your case it is better to have
var dateString = "2014-02-26T15:52:30" + "Z";
var date = new Date(dateString);
date.getUTCHours();