Javascript 在 JS 中获取客户端时区(不是 GMT 偏移量)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2897478/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get client timezone (not GMT offset amount) in JS
提问by Rex M
I need to determine the client timezone (e.g. CET, GMT, EST) in JS. Getting the offset is straightforward, but doesn't have all the info necessary to determine the TZ, at least not easily. I have a feeling it mightbe possible to use a combination of offset, whether DST is in use and in effect, etc. but I'm hoping someone else has done the work already, especially considering the weird exceptions when dealing with time.
我需要在 JS 中确定客户端时区(例如 CET、GMT、EST)。获取偏移量很简单,但没有确定 TZ 所需的所有信息,至少不容易。我有一种感觉可能可以使用偏移量的组合,DST 是否正在使用和有效等。但我希望其他人已经完成了这项工作,尤其是考虑到处理时间时的奇怪异常。
This questionis similar but both the question and the answer are based on the (incorrect) assumption every browser's DateString format includes the name of the timezone - which it does not. That is an implementation detail.
这个问题很相似,但问题和答案都基于(不正确的)假设,每个浏览器的 DateString 格式都包含时区的名称 - 但它没有。这是一个实现细节。
So, with that I am currently stuck.
所以,我目前陷入困境。
采纳答案by bobince
See this question. It's not doable in the general case, but for picking a default timezone from a shortlist of likely cases, you can make a decent guess.
看到这个问题。这在一般情况下是不可行的,但是要从可能的情况的候选名单中选择默认时区,您可以做出合理的猜测。
Allow the user to override it for when you guess wrong.
允许用户在您猜错时覆盖它。
回答by rojo
You could scrape it from a date object's toString()method. new Date.toString()results in something like Wed Sep 19 2012 10:04:32 GMT-0400 (Eastern Daylight Time)in Firefox, Safari and Chrome. The data you want is there -- the capital letters within the parentheses. All that's needed is to scrape them.
您可以从日期对象的toString()方法中获取它。 new Date.toString()结果类似于Wed Sep 19 2012 10:04:32 GMT-0400 (Eastern Daylight Time)Firefox、Safari 和 Chrome。你想要的数据就在那里——括号内的大写字母。所需要的只是刮掉它们。
In the case of Internet Explorer, the result of toString()already includes the EDT.
在 Internet Explorer 的情况下,结果toString()已经包含EDT.
In the case of Opera, your only option is to settle for GMT-0400or similar. There's nothing scrape-able in the toString()method.
在 Opera 的情况下,您唯一的选择是满足于GMT-0400或类似。该toString()方法中没有任何可刮擦的东西。
var now = new Date().toString();
var TZ = now.indexOf('(') > -1 ?
now.match(/\([^\)]+\)/)[0].match(/[A-Z]/g).join('') :
now.match(/[A-Z]{3,4}/)[0];
if (TZ == "GMT" && /(GMT\W*\d{4})/.test(now)) TZ = RegExp.;
Example results:
结果示例:
Firefox:
TZ = "EDT"Safari:
TZ = "EDT"Chrome:
TZ = "EDT"IE:
TZ = "EDT"Opera:
TZ = "GMT-0400"
火狐:
TZ = "EDT"苹果浏览器:
TZ = "EDT"铬合金:
TZ = "EDT"IE:
TZ = "EDT"歌剧:
TZ = "GMT-0400"
Seems to work just as well with all the random Asian and European time zones I tried as well, returning WPST for Guam (West Pacific Standard Time), MPST for Kuala Lumpur (Malay Peninsula Standard Time), etc; and degrades peacefully to GMT+0X00 notation where the browser doesn't supply the name (Perth, for example).
似乎也适用于我尝试过的所有随机亚洲和欧洲时区,返回关岛的 WPST(西太平洋标准时间),吉隆坡的 MPST(马来半岛标准时间)等;并和平地降级为 GMT+0X00 表示法,其中浏览器不提供名称(例如 Perth)。
回答by Kevin Le - Khnle
What I would do is determining the standard and daylight offset (which sounds like you already knew. If not, you can start with this reference http://onlineaspect.com/examples/timezone/detect_timezone.jsby Josh Fraser). Determining the time zone can always be done on the server side once the standard and daylight offsets are known. A Ajax call can then be used so that no page refresh is ever needed. End result is you now have the time zone on the JavaScript side.
我要做的是确定标准和日光偏移(听起来您已经知道了。如果没有,您可以从Josh Fraser 的参考http://onlineaspect.com/examples/timezone/detect_timezone.js开始)。一旦知道标准和日光偏移,就可以始终在服务器端确定时区。然后可以使用 Ajax 调用,这样就不需要刷新页面了。最终结果是您现在在 JavaScript 端拥有时区。
回答by Felix B?hme
Starting from this answerto another question, I found that IP APIalso gives you the timezone – pretty handy:
从这个对另一个问题的回答开始,我发现IP API还为您提供了时区 - 非常方便:
$.ajax({
url: "http://ip-api.com/json",
type: 'GET',
success: function(json) {
console.log("Timezone: " + json.timezone);
},
error: function(err) {
console.log("Request failed, error= " + err);
}
});
回答by vsingh
If you just want offset, then this is much simpler
如果你只是想要抵消,那么这更简单
var curdate = new Date();
var curdate = new Date();
var offset = curdate.getTimezoneOffset();
var offset = curdate.getTimezoneOffset();

