最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Chrome does not return correct hour in javascript - Stack Overflow

programmeradmin4浏览0评论

First try is in IE 9 console:

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

returns as expected

Next try is in 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 into Chrome 30 console:

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

But the time is 09 here, it should be 07.

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

I can't use any other format than this '2013-10-24T07:32:53' that I get by JSON from C#. I need to get the hour of this timestamp, with the getHours I get the incorect value in Chrome.

Solution:

var inputHour = input.split('T')[1];
inputHour = inputHour.substring(0, 2);

First try is in IE 9 console:

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

returns as expected

Next try is in 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 into Chrome 30 console:

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

But the time is 09 here, it should be 07.

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

I can't use any other format than this '2013-10-24T07:32:53' that I get by JSON from C#. I need to get the hour of this timestamp, with the getHours I get the incorect value in Chrome.

Solution:

var inputHour = input.split('T')[1];
inputHour = inputHour.substring(0, 2);
Share Improve this question edited Oct 24, 2013 at 16:39 Charles 51.5k13 gold badges106 silver badges144 bronze badges asked Oct 24, 2013 at 5:54 RumplinRumplin 2,76822 silver badges46 bronze badges 5
  • It appears that Chrome expects you to pass it the time in GMT and it converts it to your local time. The other browsers are interpreting the string you pass as local time. I'm not sure which is correct, or if it's part of the spec. – Mike Edwards Commented Oct 24, 2013 at 6:05
  • @MikeEdwards—ECMA-262 ed 5 specifies only one string: a version of ISO 9601 that is UTC. If the timezone designator (Z) is omitted, you're back to implementation dependent (and IE 8 and lower won't correctly parse the ISO string anyway). – RobG Commented Oct 24, 2013 at 6:41
  • @Rumplin—put a "Z" on the end for ES5 pliant browsers, it will still fail in IE 8 and lower. – RobG Commented Oct 24, 2013 at 6:42
  • I did try to put the 'Z' at the end, and I still got the 09 hour. – Rumplin Commented Oct 24, 2013 at 6:44
  • It "works" for me in Chrome with or without the "Z" (v30), the string is assumed to be UTC. That is pliant with ES5: The value of an absent time zone offset is “Z”. – RobG Commented Oct 24, 2013 at 6:52
Add a ment  | 

3 Answers 3

Reset to default 6

Its no bug. The implementation of date parse function differs across browsers & so does the format of the dateString accepted by it.

However this format seems to work same across ... link:

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

If possible, try and use

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

for guaranteed results.


Regarding your format try parsing 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 to RobG for this) : The Date constructor used above expects month as 0 - 11 & since October is 10 as per date String, the month has to be modified before passing it to the constructor.

Reference.

See this thread:

Why does Date.parse give incorrect results?

It looks like the behavior of the parsing signature of the Date constructor is pletely implementation dependent.

Given:

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

in ES5 pliant browsers you could do:

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

but for patibility across all browsers in use, better to use (assuming 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)

  1. 暂无评论