如何计算JavaScript中2个时区的差异? [英] How Do I calculate the difference of 2 time zones in JavaScript?

查看:93
本文介绍了如何计算JavaScript中2个时区的差异?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,东部和中部的区别是1.我的解决方案感觉很糟糕。有更简单/更好的方法吗?

For example, the difference in Eastern and Central is 1. My solution below feels hacky. Is there a easier / better way?

var diff = (parseInt(moment().tz("America/New_York").format("ZZ")) - parseInt(moment().tz("America/Chicago").format("ZZ"))) / 100;

我的例子是使用 Momentjs 库。

推荐答案

无法计算 之间的差异两个任意时区。您只能计算特定时刻的差异

It's impossible to calculate the difference between two arbitrary time zones. You can only calculate a difference for a specific moment in time.


  • 目前伦敦和纽约之间有4个小时的差异(2015年3月25日写的这个)。

  • 但几周之前是5个小时的差异,而且几个星期后它将是5个小时。现在。

  • 每个时区都会在不同的时间点切换夏令时的偏移量。

  • It's currently 4 hours difference between London and New York (writing this on Mar 25, 2015).
  • But it was 5 hours difference a few weeks ago, and it will be 5 a few weeks from now.
  • Each time zone switches offsets for daylight saving time at a different point in time.

两个时区之间的一般情况都是如此。但是某些时区要么同时完全切换,要么根本不切换。

This is true in the general case between two time zones. However some time zones either switch exactly at the same time, or don't switch at all.


  • 那里在伦敦和巴黎之间总是一小时,因为它们会在同一时刻切换。

  • 总是 3小时之间亚利桑那州和夏威夷,因为都没有夏令时。

  • There is always one hour between London and Paris, because they switch at the same moment in time.
  • There are always 3 hours between Arizona and Hawaii, because neither have DST.

请记住,在美国,每个使用DST的时区实际上都会切换到一个不同的时刻。它们都在凌晨2点在本地时间切换,但不会在同一时间通用时刻切换。

Keep in mind that in the United States, each time zone that uses DST actually switches at a different moment in time. They all switch at 2:00 AM in their local time, but not at the same universal moment in time.


  • 所以在芝加哥和纽约之间,通常相隔1小时

  • 但是每年两个短暂的时期他们要么是相隔2小时,或同一时间。

  • So between Chicago and New York, there is usually 1 hour apart
  • But for two brief periods each year they are either 2 hours apart, or have the same exact time.

另请参阅时区!=偏移在时区标签维基

See also "Time Zone != Offset" in the timezone tag wiki.

现在关于时刻-timezone,你在评论中说:

Now with regard to moment-timezone, you said in comments:


网络服务器位于东部时区;我们在中环。我需要用户时区和服务器的区别。

Web服务器的时区无关紧要。您应该能够在世界任何地方托管而不会影响您的应用程序。如果你做不到,那你做错了。

The time zone of the web server is irrelevant. You should be able to host from anywhere in the world without affecting your application. If you can't, then you're doing it wrong.

可以获得当前时差在您的时区(美国中部时间)和用户之间。如果代码在浏览器中运行,您甚至不需要知道用户的确切时区:

You can get the current time difference between your time zone (US Central time) and the user's. You don't even need to know the user's exact time zone for this, if the code is running in the browser:

var now = moment();
var localOffset = now.utcOffset();
now.tz("America/Chicago"); // your time zone, not necessarily the server's
var centralOffset = now.utcOffset();
var diffInMinutes = localOffset - centralOffset;

如果代码在服务器上运行(在node.js应用程序中),那么你需要知道用户的时区。只需像这样更改第一行:

If instead the code was running on the server (in a node.js app), then you would need to know the user's time zone. Just change the first line like this:

var now = moment.tz("America/New_York"); // their time zone

这篇关于如何计算JavaScript中2个时区的差异?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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