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

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

问题描述

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

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

我都需要:

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

推荐答案

使用getTimezoneOffset()

您可以在几分钟之内获得时区偏移,如下所示:

Using getTimezoneOffset()

You can get the time zone offset in minutes like this:

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日期对象参考
    • Mozilla Date Object reference
    • 请注意,并非所有时区都可以被整个小时抵消:例如,纽芬兰是UTC减去3h 30m(不考虑夏令时).

      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天全站免登陆