Chrome浏览器不会在JavaScript中返回正确的小时数 [英] Chrome does not return correct hour in javascript

查看:159
本文介绍了Chrome浏览器不会在JavaScript中返回正确的小时数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先尝试在IE 9控制台中:

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

按预期收益

 $ b 

-10-24T07:32:53')
日期{Thu Oct 24 2013 07:32:53 GMT + 0200(中欧标准时间)}

然后我进入Chrome 30控制台:

  new Date(' (中欧夏令时)

但现在时间 09 ,应该是 07



我不能使用除此之外的任何其他格式'2013-10-24T07:32 :53',我从C#获得JSON。
我需要得到这个时间戳的小时, getHours 我在Chrome中获得incorect值。



解决方案:

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


解决方案

日期解析功能的实现在浏览器和浏览器中是不同的。它接受的 dateString 的格式也是如此。



然而,这种格式似乎在... link

 新日期(1975年10月13日11:13:00)

如果可能,请尝试使用

 新日期(年,月,日,小时,分,秒,毫秒)
$ b


$ b 关于你的格式尝试解析它自己。例如:

  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);






注意(感谢RobG for这个):上面使用的Date构造函数预计月份为0 - 11&由于10月份的日期字符串为10,因此必须在将该月份传递给构造函数之前对其进行修改。



参考资料


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);

解决方案

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.

这篇关于Chrome浏览器不会在JavaScript中返回正确的小时数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆