0

I need to display the the current time in the Central Timzone, reguardless of the client's local timezone, and to do that I need to know if the Central timezone is at UTC-6 or UTC-5

What is the best way to handle daylight savings in Javascript?

4

3 回答 3

1

在检查了这篇文章后,我最终在服务器上执行此操作并将其传递给客户端: 夏令时和时区最佳实践

Never trust client datetime. It may very well be incorrect.
于 2013-10-10T14:23:32.427 回答
1

首先,您需要一个实现 IANA 时区数据库的 JavaScript 库。我在这里列出了其中的几个。

美国中部时间的 IANA 区域名称是America/Chicago

如果您只想知道该区域的当前时间,并且适当地应用了 DST,那么所有这些库都将某种形式作为其主要用例。

可能最简单的例子是moment-timezone,您可以简单地这样做:

var nowInCentralTime = moment().tz("America/Chicago");
var stringToOuput = nowInCentralTime.format("LLL");

(当然,您可以应用您想要的任何格式。有关详细信息,请参阅moment.js文档)。

如果你想知道这是否是 DST,你可以这样做:

var centralIsInDST = nowInCentralTime.isDST();
于 2013-10-10T02:47:41.293 回答
0

当当地地区明显不是DST时找到偏移量

var zone,
    z1 = new Date(2013, 0, 1).getTimezoneOffset(),
    z2 = new Date(2013, 5, 1).getTimezoneOffset();
if (z2 < z1) // northern hemisphere
    zone = z1;
else         // southern hemisphere
    zone = z2;
于 2013-10-09T17:50:59.267 回答