javascript - 自动检测设备时区的准确度如何?

标签 javascript timezone

在这里查看本教程 http://pellepim.bitbucket.org/jstz/使用 JavaScript 进行自动时区检测,检测设备的时区设置。

我想知道是否有人知道(大概)这种方法检测时区的准确度如何?

感谢您的宝贵时间!

最佳答案

来自 the project page 的“限制”部分:

This script does not do geo-location, nor does it care very much about historical time zones.

So if you are unhappy with the time zone "Europe/Berlin" when the user is in fact in "Europe/Stockholm" - this script is not for you. (They are both identical in modern time).

Also, if it is important to you to know that in Europe/Simferopool (Ukraine) the UTC offset before 1924 was +2.67, sorry, this script will not help you.

Time zones are a screwed up thing, generally speaking, and the scope of this script is to solve problems concerning modern time zones, in this case from 2010 and forward.

您可以在the project's issue tracker中看到一些检测不完善的问题。 .

从设计 Angular 来看,它试图通过使用 getTimezoneOffset() 在不同时间点获取多个读数并将其与已知值进行比较来尝试做正确的事情。

最大的问题是JavaScript lies 。它的 API 会让您认为您可以检查任何日期/时间的本地偏移量。但实际上,它只知道时区的当前规则。由于不同的时区在不同的时间点发生变化,因此很难知道您是否正在使用“当前”时区规则,除非您专门检查“现在”。这是在 issue #68 中提出的,但被决定超出范围。

另请注意,如果您测试当前规则,通常会有几个不同的时区重叠。请参阅issue #74issue #64 .

随着时区规则随着时间的推移而变化,必须发布新版本的 jstzDetect 来进行补偿。使用它的应用程序也必须计划更新。到目前为止,还没有太多更新。

不幸的是,没有一种完美的方法来检测用户的时区。一些浏览器开始支持以下内置机制:

Intl.DateTimeFormat().resolvedOptions().timeZone

但不幸的是它还没有被广泛采用 there are bugs there too .

我能提供的最佳建议是使用 jstzDetect 作为起点。它擅长对应用程序中的其他时区选择控件进行初步猜测。如果您关心准确性,请不要将其用于更多用途。

See also, this related answer.

关于javascript - 自动检测设备时区的准确度如何?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23253103/

相关文章:

javascript - 使用 javascript 填充输入字段的值

JavaScript:GUID 冲突

c# - 使用 Google Maps API 和 GeoXML3 导入并显示本地 KML 文件

javascript - 将 jquery-ui-timepicker.js 的 'Now' 按钮设置为 UTC 时区

postgresql - Grafana/Postgresql : how to convert date stored in UTC to local timezone

javascript - 如何向 cal-heatmap 中的子域添加标签?

javascript - 从 Javascript 类中的静态方法引用成员函数的正确方法

java - 如何在末尾没有时区的情况下将日期时间显示为本地时间?

ios - 如何在uitableview中获取这种类型的时区列表?

PHP DateTime 时区 - 构造函数与 Setter 方法