为什么我从函数得到错误的结果? [英] Why I get wrong result from function?
问题描述
我有这个对象数组:
var data = [{Id:540,MeasureDate:2016-06-27T15 :06:47},
{Id:541,MeasureDate:2016-06-27T12:24:39}];
我调用此函数:
var latestdate = updateLatestDate(data);
function updateLatestDate(sensorsData){
return new Date(Math.max.apply(null,sensorsData.map(function(e){
return new Date(e.MeasureDate );
})))。toISOString();
}
函数 updateLatestDate
返回最新日期并将其转换为 ISOString()
。
但问题是,
在chrome中, latestdate
变量是2016-06- 27T15:06:47.000Z
在IE中, latestdate
变量为2016 -06-27T12:06:47.000Z
功能 updateLatestDate
c $ c> chrome 但它在 IE
中返回错误的结果,在我看来问题可能在于 Math .max.apply
函数。
任何想法为什么我在IE中得到错误的结果, p>
提前感谢!
具有在ISO8601时间戳中指定的时区。当默认情况下,Chrome默认为UTC,而IE为local,当没有指定时(或者相反,我忘了)。
您可以:
a)在时间戳的末尾加上Z添加其他时区(有关格式,请参阅wikipedia https://en.m.wikipedia.org/wiki/ ISO_8601 )
b)使用momentjs,我相信它会标准化整个浏览器的行为。
I has this objects array:
var data=[{Id:540, MeasureDate:"2016-06-27T15:06:47"},
{Id:541, MeasureDate:"2016-06-27T12:24:39"}];
I call this function:
var latestdate = updateLatestDate(data);
function updateLatestDate(sensorsData) {
return new Date(Math.max.apply(null, sensorsData.map(function (e) {
return new Date(e.MeasureDate);
}))).toISOString();
}
the function updateLatestDate
returns latest date and convert it to ISOString()
.
But the problem is that I get diffrent results if I use it in chrome and in IE11.
In chrome the latestdate
variable is "2016-06-27T15:06:47.000Z"
In IE the latestdate
variable is "2016-06-27T12:06:47.000Z"
The function updateLatestDate
works perfect in chrome
but it returns wrong result in IE
, it seems to me that the problem might be with Math.max.apply
function.
Any idea why I get wrong result in IE and how can I fix it?
Thank you in advance!
The reason is you don't have a timezone specified in your ISO8601 timestamp. Chrome defaults to UTC and and IE to local when it is not specified (or the other way around, I forget).
You can either:
a) add 'Z' to the end of your timestamp to specify UTC or add another timezone ( see wikipedia for valid formats https://en.m.wikipedia.org/wiki/ISO_8601)
b) use momentjs which I believe will standardize the behavior across the browsers.
这篇关于为什么我从函数得到错误的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!