How can I get the timezone name in JavaScript?(如何在 JavaScript 中获取时区名称?)
问题描述
我知道如何获取时区偏移量,但我需要的是能够检测美国/纽约"之类的内容.这甚至可以通过 JavaScript 实现,还是我必须根据偏移量来猜测?
I know how to get the timezone offset, but what I need is the ability to detect something like "America/New York." Is that even possible from JavaScript or is that something I am going to have to guestimate based on the offset?
推荐答案
国际化 API 支持获取用户时区,支持在所有当前的浏览器中.
The Internationalization API supports getting the user timezone, and is supported in all current browsers.
console.log(Intl.DateTimeFormat().resolvedOptions().timeZone)
请记住,在一些支持国际化 API 的旧浏览器版本上,timeZone
属性设置为 undefined
而不是用户的时区字符串.据我所知,在撰写本文时(2017 年 7 月)除 IE11 之外的所有当前浏览器 都会将用户时区作为字符串返回.
Keep in mind that on some older browser versions that support the Internationalization API, the timeZone
property is set to undefined
rather than the user’s timezone string. As best as I can tell, at the time of writing (July 2017) all current browsers except for IE11 will return the user timezone as a string.
这篇关于如何在 JavaScript 中获取时区名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 JavaScript 中获取时区名称?


基础教程推荐
- npm start 错误与 create-react-app 2022-01-01
- 原生拖动事件后如何获取 mouseup 事件? 2022-01-01
- Bokeh Div文本对齐 2022-01-01
- 即使用户允许,Gmail 也会隐藏外部电子邮件图片 2022-01-01
- 如何添加到目前为止的天数? 2022-01-01
- Fabric JS绘制具有活动形状的多边形 2022-01-01
- Bootstrap 模态出现在背景下 2022-01-01
- 检查 HTML5 拖放文件类型 2022-01-01
- 在 contenteditable 中精确拖放 2022-01-01
- fetch 是否支持原生多文件上传? 2022-01-01