Chrome does not return the correct hour in javascript

The first attempt is in the IE 9 console:

new Date('2013-10-24T07:32:53') Thu Oct 24 07:32:53 UTC+0200 2013 

returns as expected

The following attempt is in the FireFox 24 console:

 new Date('2013-10-24T07:32:53') Date {Thu Oct 24 2013 07:32:53 GMT+0200 (Central Europe Standard Time)} 

Then I go to the Chrome 30 console:

 new Date('2013-10-24T07:32:53') Thu Oct 24 2013 09:32:53 GMT+0200 (Central Europe Daylight Time) 

But time is 09 , it should be 07 .

Is this a bug in chrome or am I doing something wrong here?

I cannot use any other format than this "2013-10-24T07: 32: 53", which I get from JSON with C #. I need to get the hour of this timestamp, with getHours I get the insect value in Chrome.

Decision:

 var inputHour = input.split('T')[1]; inputHour = inputHour.substring(0, 2); 
+2
javascript google-chrome time
source share
3 answers

There is no mistake. The implementation of the date syntax function differs in different browsers, and also accepts the String date format.

However, this format works the same in ... link :

  new Date("October 13, 1975 11:13:00") 

If possible, try using

 new Date(year, month, day, hours, minutes, seconds, milliseconds) 

for guaranteed results.


As for your format, try to parse it yourself. Something like:

 var str = '2013-10-24T07:32:53'.split("T"); var date = str[0].split("-"); var time = str[1].split(":"); var myDate = new Date(date[0], date[1]-1, date[2], time[0], time[1], time[2], 0); 

Note. (Thanks RobG for this): The Date constructor used above expects the month to be 0 - 11, and from October to 10 by the String date, the month must be changed before passing it to the constructor.

Reference .

+5
source share

See this topic:

Why does Date.parse give incorrect results?

It appears that the behavior of the parser signature of the Date constructor is entirely implementation dependent.

+2
source share

Given:

 var s = '2013-10-24T07:32:53'; 

in ES5 compatible browsers you can do:

 var d = new Date(s + 'Z'); 

but for compatibility in all browsers used, it is better to use (provided that the date is UTC):

 function dateFromString(s) { s = s.split(/\D/); return new Date(Date.UTC(s[0],--s[1],s[2],s[3],s[4],s[5])); } 
0
source share

All Articles