How to get browser time zone in JavaScript

天涯浪子 提交于 2019-12-23 03:39:33

问题


Is it possible to get the user time zone in JavaSript in tz database format e.g. America/Los Angeles?

I noticed that when I create a Date object its toString method returns something like this:

Thu May 08 2014 08:40:48 GMT-0700 (Pacific Standard Time)

so maybe it's possible to change it to one of ids in tz database from this region (unfortunately chrome shows Pacific Daylight Time so it seems like there's no standard format to display). I need some library because I don't have time to right it myself with all possible combination and discrepancies in browsers like firefox vs chrome.


回答1:


From: http://www.w3schools.com/jsref/jsref_gettimezoneoffset.asp

Return the timezone difference between UTC and Local Time:

var d = new Date()
var n = d.getTimezoneOffset();

The result n will be 420. Divide that by 60 to difference in hours.

You could then create an array populated text so that:

 timezoneArray[n] = "tz text description";


来源:https://stackoverflow.com/questions/23546755/how-to-get-browser-time-zone-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!