4

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.

darksoulsong
  • 13,988
  • 14
  • 47
  • 90
  • 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 Feb 26 '14 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 Feb 26 '14 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 Feb 26 '14 at 20:06
  • possible duplicate of [Chrome interprets ISO time without Z as UTC; C# issue](http://stackoverflow.com/questions/14923932/chrome-interprets-iso-time-without-z-as-utc-c-sharp-issue) – Praveen Feb 26 '14 at 20:13
  • 1
    @Praveen Thanks, that confirms that Chrome is getting it right (yay Chrome). – aknuds1 Feb 26 '14 at 20:13

2 Answers2

6

Chrome 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 computing need and perform the timezone transformation when you want to show the date to the user. Timezone can become a real pain.

Nico
  • 6,395
  • 4
  • 25
  • 34
  • This is what I was going to say, although the [ECMAScript standard](http://www.ecma-international.org/ecma-262/5.1/#sec-15.9.1.15) says that an absent timezone is supposed to be treated as 'Z', so it's strange that FF and IE do it differently. – aknuds1 Feb 26 '14 at 20:04
  • Like a boss. I couldn't find that info anywhere. I'll simply add the "Z" to the end of the string to deal with this. – darksoulsong Feb 26 '14 at 20:17
  • God damn browsers... Thanks for this :D I did the same, added Z to ensure they all treat it as UTC – Darren Wainwright Aug 20 '15 at 18:48
3

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 common 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();
Praveen
  • 55,303
  • 33
  • 133
  • 164