在 JavaScript 中获取客户端的时区(和偏移量) [英] Getting the client's time zone (and offset) in JavaScript

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

问题描述

如何收集访问者的时区信息?

How can I gather the visitor's time zone information?

我需要:

  1. 时区(例如,欧洲/伦敦)
  2. 以及与 UTC 或 GMT 的偏移量(例如,UTC+01)

推荐答案

Using getTimezoneOffset()

您可以像这样在几分钟内获得时区偏移:

var offset = new Date().getTimezoneOffset();
console.log(offset);
// if offset equals -60 then the time zone offset is UTC+01

时区偏移量是 UTC 和本地时间之间的时差(以分钟为单位).请注意,这意味着如果本地时区落后于 UTC,则偏移量为正,如果领先则为负.例如,如果您的时区是 UTC+10(澳大利亚东部标准时间),则返回 -600.即使对于给定的语言环境,夏令时也会阻止此值保持不变

The time-zone offset is the difference, in minutes, between UTC and local time. Note that this means that the offset is positive if the local timezone is behind UTC and negative if it is ahead. For example, if your time zone is UTC+10 (Australian Eastern Standard Time), -600 will be returned. Daylight savings time prevents this value from being a constant even for a given locale

  • Mozilla 日期对象参考
  • 请注意,并非所有时区都以整小时为单位偏移:例如,纽芬兰是 UTC 减去 3 小时 30 米(不考虑夏令时).

    Note that not all timezones are offset by whole hours: for example, Newfoundland is UTC minus 3h 30m (leaving Daylight Saving Time out of the equation).

    另请注意,这只会为您提供时区偏移量(例如:UTC+01),而不会为您提供时区(例如:欧洲/伦敦).

    Please also note that this only gives you the time zone offset (eg: UTC+01), it does not give you the time zone (eg: Europe/London).

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

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