Date.getUTCHours () - Various output in Chrome, Firefox, and Internet Explorer

Given the following code:

var date = new Date("2014-02-26T15:52:30");
date.getUTCHours();
// Outputs
// Chrome: 15
// Firefox: 18
// IE: 18

I am not sure which date method I should use. Date.getHoursreturns the correct hour in FF and IE, but is incorrect in Chrome. And it Date.getUTCHours()shows me the correct date, but out of order in both IE and FF. What is the difference? What UTC date should be?

I wonder if there is a native CrossBrowser solution ... I would not want to use a library for such a simple thing.

+3
source share
2 answers

Chrome interprets your date the var date = new Date("2014-02-26T15:52:30");same way var date = new Date("2014-02-26T15:52:30Z");(pay attention to z, indicating the time zone of utc).

FF IE var date = new Date("2014-02-26T15:52:30"); , var date = new Date("2014-02-26T15:52:30-05:00"); (GMT-0500)

UTC , . .

+6

getUTCHours() dateString (2014-02-26T15:52:30).

ISO8601,

var date = new Date("2014-02-26T15:52:30Z");

var date = new Date("2014-02-26T15:52:30+00:00");

.

var dateString = "2014-02-26T15:52:30" + "Z";
var date = new Date(dateString);
date.getUTCHours();
+3

All Articles