如何使用 moment-timezone.js 获取客户端时区

How to get the client timezone using moment-timezone.js

如何使用 moment-timezone.js 或 moment.js 获取客户端时区?

谢谢!

此功能当前不可用,但计划在未来版本中提供。参见:

同时,考虑jsTimeZoneDetect

另请参阅:Can I get the name of the time zone of the client by using jQuery?

查看上面的问题列表,我发现检测用户时区是可用的,虽然不是很正确。

moment.tz.guess()
>> "Europe/Budapest"    // when actually I am in Madrid, but I think it is due to some historical reason

根据讨论并检查了 src (0.5),它基于 Intl.DateTimeFormat().resolvedOptions().timeZone,被认为是最正确的方式,并被许多现代(适当的)浏览器支持,请参阅列表:https://caniuse.com/#feat=internationalization

幸运的是,布达佩斯使用与马德里相同的夏令时时间。如果您的目标用户群分布在更广泛的地域,我建议您采取更多的预防措施。