在JavaScript中获取客户的GMT偏移量 [英] get client's GMT offset in javascript

查看:54
本文介绍了在JavaScript中获取客户的GMT偏移量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获取客户端javascript中的GMT偏移量?

how can I get the GMT offset in javascript of the client?

new Date().getTimezoneOffset(); 返回与UTC的差异.有什么方法可以据此计算GMT偏移量吗?

new Date().getTimezoneOffset(); returns the difference from UTC. Is there a way I can calculate the GMT offset from that?

按格林尼治标准时间偏移,我的意思是东部标准时间为-5.

By GMT offset, I mean as in -5 for eastern standard time.

推荐答案

new Date().getTimezoneOffset();从UTC返回差额.有什么方法可以据此计算GMT偏移量吗?

new Date().getTimezoneOffset(); returns the difference from UTC. Is there a way I can calculate the GMT offset from that?

时区偏移量是与格林威治标准时间的差异,以分钟为单位(请参见 ECMA-262§15.9.5.26).该标志与ISO 8601相反,但是可以使用更标准的标志轻松转换为小时和分钟:

The timezone offset is the difference from GMT in minutes (see ECMA-262 §15.9.5.26). The sign is the reverse of ISO 8601, but it's easily converted to hours and minutes with a more standard sign:

function getTimezoneOffset() {
  function z(n){return (n<10? '0' : '') + n}
  var offset = new Date().getTimezoneOffset();
  var sign = offset < 0? '+' : '-';
  offset = Math.abs(offset);
  return sign + z(offset/60 | 0) + z(offset%60);
}

getTimezoneOffset() // +0800 for UTC/GMT + 8hrs

如果您想确定IANA时区名称,可以尝试 pellepim jstimezonedetect ,但是它可以通过基于两个日期的偏移量进行猜测来工作,并且可能不正确.

If you want to determine the IANA timezone designation, you can try pellepim jstimezonedetect, however it works by guessing based on the offset for two dates and may not be correct.

这篇关于在JavaScript中获取客户的GMT偏移量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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