javascript - 在 JavaScript 中获取客户端的时区(和偏移量)

标签 javascript timezone

如何收集访问者的时区信息?

两个都需要:

  1. 时区(例如,欧洲/伦敦)
  2. 以及与 UTC 或 GMT 的偏移量(例如,UTC+01)

最佳答案

使用偏移量来计算时区是错误的做法,总会遇到问题。时区和夏令时规则在一年中可能会发生多次变化,而且很难跟上变化。

获取系统的IANA timezone在 JavaScript 中,你应该使用

console.log(Intl.DateTimeFormat().resolvedOptions().timeZone)

截至 2022 年 4 月,此 works in 93.5% of the browsers used globally .

旧的兼容性信息

ecma-402/1.0 表示如果没有提供给构造函数,timeZone 可能是未定义的。但是, future 的草案 (3.0) 通过更改为系统默认时区解决了该问题。

In this version of the ECMAScript Internationalization API, the timeZone property will remain undefined if no timeZone property was provided in the options object provided to the Intl.DateTimeFormat constructor. However, applications should not rely on this, as future versions may return a String value identifying the host environment’s current time zone instead.

在 ecma-402/3.0 中,它仍处于草案中,它更改为

In this version of the ECMAScript 2015 Internationalization API, the timeZone property will be the name of the default time zone if no timeZone property was provided in the options object provided to the Intl.DateTimeFormat constructor. The previous version left the timeZone property undefined in this case.

关于javascript - 在 JavaScript 中获取客户端的时区(和偏移量),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1091372/

相关文章:

javascript - React 应用程序无法仅在 Windows 上处理 svg 图像

php - 应用从数据库获取DATETIME-时区问题

date - php5.3.3 date.timezone 再次未考虑 php.ini 指令

python - 具有系统时区设置的 Django 与用户的个人时区

javascript - CryptoJS 和 Pycrypto 协同工作

javascript - 强制 Ajax 获取缓存版本

数组选择中的 Javascript 对象

ios - 为什么我在正确的时区得到错误的时间输出?

asp.net - 没有时区信息的 Javascript ASP.net 日期格式 - 时区偏移

JavaScript - 需要多维数组吗?