問(wèn)題描述
我知道如何獲取時(shí)區(qū)偏移量,但我需要的是能夠檢測(cè)美國(guó)/紐約"之類的內(nèi)容.這甚至可以通過(guò) JavaScript 實(shí)現(xiàn),還是我必須根據(jù)偏移量來(lái)猜測(cè)?
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?
推薦答案
國(guó)際化 API 支持獲取用戶時(shí)區(qū),支持在所有當(dāng)前的瀏覽器中.
The Internationalization API supports getting the user timezone, and is supported in all current browsers.
console.log(Intl.DateTimeFormat().resolvedOptions().timeZone)
請(qǐng)記住,在一些支持國(guó)際化 API 的舊瀏覽器版本上,timeZone
屬性設(shè)置為 undefined
而不是用戶的時(shí)區(qū)字符串.據(jù)我所知,在撰寫(xiě)本文時(shí)(2017 年 7 月)除 IE11 之外的所有當(dāng)前瀏覽器 都會(huì)將用戶時(shí)區(qū)作為字符串返回.
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.
這篇關(guān)于如何在 JavaScript 中獲取時(shí)區(qū)名稱?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!