在 JavaScript 中计算两个日期之间的差异时出现时区问题
我使用以下代码来计算 JavaScript 中两个日期之间的差异:
var dateOne = new Date; // Now
var dateTwo = new Date( dateOne.getTime() + 60 * 1000 ); // Now + One Minute
var difference = new Date( dateTwo - dateOne );
因此,从逻辑上讲,差异应该是一分钟。但 Firebug 告诉我,相差一小时,而且时区也以某种方式发生了变化!
dateOne = Date {Sun Sep 11 2011 01:07:55 GMT+0200 (CET)}
dateTwo = Date {Sun Sep 11 2011 01:08:55 GMT+0200 (CET)}
difference = Date {Thu Jan 01 1970 01:01:00 GMT+0100 (CET)}
我该如何解决这个问题?
I use the following to calculate the difference between two dates in JavaScript:
var dateOne = new Date; // Now
var dateTwo = new Date( dateOne.getTime() + 60 * 1000 ); // Now + One Minute
var difference = new Date( dateTwo - dateOne );
So, logically, difference
should be one minute. But Firebug tells me that the difference is one hour off, and the timezone somehow also changes!
dateOne = Date {Sun Sep 11 2011 01:07:55 GMT+0200 (CET)}
dateTwo = Date {Sun Sep 11 2011 01:08:55 GMT+0200 (CET)}
difference = Date {Thu Jan 01 1970 01:01:00 GMT+0100 (CET)}
How can I fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Date
旨在存储准确的日期和时间,而不是日期和时间之间的差异。减去这些Date
对象即可得出这两个日期之间的毫秒数。然后,您可以使用自纪元以来的毫秒数创建一个新的Date
。由于纪元是 1970 年 1 月 1 日午夜,因此结果将是 1970 年 1 月 1 日的凌晨 12:01。夏令时会稍微改变时区。Date
is designed for storing exact dates and times, not differences between dates and times. Subtracting thoseDate
objects yields the number of milliseconds between those two dates. You then create a newDate
with that number of milliseconds since the Epoch. Since the Epoch is midnight of January 1, 1970, the result will be 12:01 AM of January 1, 1970. Daylight savings time changes the timezone a little.在 JavaScript 中,所有时间均采用 GMT。每次将
Date
转换为字符串时,时区都会“应用”到输出中。所以你可以尝试得到以毫秒为单位的差异:自从你添加后,这显然是 60000。
没有 TimeSpan 类或其他东西。日期仅存储日期。
编辑:
如果您想知道为什么输出为 +2:00 小时,然后输出为 +1:00,那是因为 1 月 1 日采用标准时间,而 9 月 11 日采用夏令时。这并不是因为 JavaScript 在从一个日期减去另一个日期时做了一些有趣的事情。
In JavaScript all times are in GMT. Every time you convert a
Date
to a string the timezone is "applied" to the output. So you can try to get the difference in milliseconds:That's obviously 60000 since you added that.
There is no TimeSpan class or something. Dates only store dates.
EDIT:
If you were wondering why there was the output of +2:00 hours and then just +1:00 that's because January 1st is in Standard Time whereas September 11th has Summer Saving Time. It's not because JavaScript does something funny when deducting one date from another.
前面的答案很好地阐明了问题的原因,但我认为指出 Javascript 同时具有“本机时区”和“UTC”方法也是有意义的:-
...如果运行该代码的机器有本地时区GMT 以外的时区(可能是英国夏令时,即 GMT+01:00),那么返回值可能不同(取决于您设置的日期值以及 JS 引擎是否执行此操作)返回 GMT从非 UTC 方法 - 我看到了差异,例如在 Rhino 中存在差异)。
因此,为了计算的目的,您可能希望使用 UTC 方法提取所有值,这确保一切都基于 GMT。这将使数学“在技术上是正确的”,但对于给定的目的,这在语义上是否正确取决于用例。
例如,美国凌晨 2 点与英国凌晨 2 点相比,忽略时区,为 0。而如果考虑时区,则可能为 6(例如)。有时我也会为此苦苦挣扎,并且在我选择一条或另一条路线之前,常常必须认真思考我想要实现的目标!总是把我的面条烤一会儿:-)
The previous answers are very good at clarifying the cause of the problem but I think it also makes sense to point out that Javascript has both "native timezone" and "UTC" methods:-
...If the machine running that code has a local timezone other than GMT (perhaps UK Daylight Savings which is GMT+01:00), then the return values could be different (depending on the date value you have set and whether the JS engine does or doesn't return GMT from the non-UTC method - i have seen differences, for example in Rhino there IS a difference).
Therefore for the purpose of the calculations you MAY wish to extract all values using the UTC methods, which ensures everything is based upon GMT. That would make the maths "technically correct", but whether that is semantically correct for the given purpose depends on the use case.
For example, 2am in USA compared to 2am in UK, ignoring timezones, is 0. Whereas with timezones it could be 6 (for example). I sometimes struggle with this too and often have to think hard about what I'm trying to achieve before I following one route or another! Always bakes my noodle for a little while :-)
您可以通过以下公式计算两个日期之间的两个日期之间的完整证明天差:
you can calculate a full proof days difference between two dates resting across TZs by the following formula: