如何收集访问者的时区信息?
两个都需要:
- 时区(例如,欧洲/伦敦)
- 以及与 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 notimeZone
property was provided in the options object provided to theIntl.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 notimeZone
property was provided in the options object provided to theIntl.DateTimeFormat
constructor. The previous version left thetimeZone
property undefined in this case.
关于javascript - 在 JavaScript 中获取客户端的时区(和偏移量),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1091372/