为什么我从功能中得到错误的结果? [英] Why do I get wrong result from function?

查看:132
本文介绍了为什么我从功能中得到错误的结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个对象数组:

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

the function updateLatestDate returns latest date and convert it to ISOString().

但问题是如果我得到不同的结果在chrome和IE11中使用它。

But the problem is that I get diffrent results if I use it in chrome and in IE11.

在chrome中, latestdate 变量是2016-06- 27T15:06:47.000Z

In chrome the latestdate variable is "2016-06-27T15:06:47.000Z"

在IE中, latestdate 变量是2016 -06-27T12:06:47.000Z

函数 updateLatestDate chrome 但它在 IE 中返回错误的结果,在我看来问题可能出在 Math .max.apply
函数。

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.

我知道为什么我的错误导致IE导致我如何解决?

Any idea why I get wrong result in IE and how can I fix it?

推荐答案

原因是您没有在ISO8601时间戳中指定时区。当Chrome未指定时,Chrome默认为UTC,IE默认为本地(反之,我忘了)。

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

您可以:

a)在时间戳的末尾添加Z以指定UTC或添加另一个时区(请参阅维基百科的有效格式 https://en.m.wikipedia.org/wiki/ ISO_8601

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)使用我认为会标准化浏览器行为的momentjs。

b) use momentjs which I believe will standardize the behavior across the browsers.

这篇关于为什么我从功能中得到错误的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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