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