如何在JavaScript中获取时区名称? [英] How can I get the timezone name in JavaScript?

查看:53
本文介绍了如何在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?

推荐答案

受支持在当前所有的浏览器中.

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月)

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中获取时区名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆