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 中获取时区名称?


基础教程推荐
- 我可以在浏览器中与Babel一起使用ES模块,而不捆绑我的代码吗? 2022-01-01
- 直接将值设置为滑块 2022-01-01
- 用于 Twitter 小部件宽度的 HTML/CSS 2022-01-01
- 自定义 XMLHttpRequest.prototype.open 2022-01-01
- Chart.js 在线性图表上拖动点 2022-01-01
- 如何使用JIT在顺风css中使用布局变体? 2022-01-01
- Vue 3 – <过渡>渲染不能动画的非元素根节点 2022-01-01
- Electron 将 Node.js 和 Chromium 上下文结合起来意味着 2022-01-01
- html表格如何通过更改悬停边框来突出显示列? 2022-01-01
- 如何使用TypeScrip将固定承诺数组中的项设置为可选 2022-01-01