【问题标题】:How can I get the timezone name in JavaScript?如何在 JavaScript 中获取时区名称?
【发布时间】:2012-04-04 02:27:58
【问题描述】:

我知道如何获取时区偏移量,但我需要的是能够检测“美国/纽约”之类的内容。这甚至可以通过 JavaScript 实现,还是我必须根据偏移量来猜测?

【问题讨论】:

标签: javascript timezone


【解决方案1】:

您可以使用此处的映射表简单地编写自己的代码: http://www.timeanddate.com/time/zones/

或者,使用时刻时区库: http://momentjs.com/timezone/docs/

zone.name; // America/Los_Angeles

或者,这个库: https://github.com/Canop/tzdetect.js

【讨论】:

  • 不是特别有用,因为多个时区具有相同的偏移量
【解决方案2】:

您可以使用此脚本。 http://pellepim.bitbucket.org/jstz/

在此处分叉或克隆存储库。 https://bitbucket.org/pellepim/jstimezonedetect

包含脚本后,您可以在 - jstz.olson.timezones 变量中获取时区列表。

以下代码用于确定客户端浏览器的时区。

var tz = jstz.determine();
tz.name();

享受 jstz!

【讨论】:

    【解决方案3】:

    在 javascript 中,Date.getTimezoneOffset() 方法返回当前语言环境与 UTC 的时区偏移量(以分钟为单位)。

    var x = new Date();
    var currentTimeZoneOffsetInHours = x.getTimezoneOffset() / 60;
    

    Moment'timezone 将是一个有用的工具。 http://momentjs.com/timezone/

    在时区之间转换日期

    var newYork    = moment.tz("2014-06-01 12:00", "America/New_York");
    var losAngeles = newYork.clone().tz("America/Los_Angeles");
    var london     = newYork.clone().tz("Europe/London");
    
    newYork.format();    // 2014-06-01T12:00:00-04:00
    losAngeles.format(); // 2014-06-01T09:00:00-07:00
    london.format();     // 2014-06-01T17:00:00+01:00
    

    【讨论】:

    • 你不懂题。
    • 您的代码示例与原始问题无关。你将如何使用这个库来返回用户当前时区的名称?
    【解决方案4】:

    Internationalization API 支持获取用户时区,在当前所有浏览器中为supported

    console.log(Intl.DateTimeFormat().resolvedOptions().timeZone)

    请记住,在一些支持国际化 API 的旧浏览器版本上,timeZone 属性设置为undefined,而不是用户的时区字符串。据我所知,在撰写本文时(2017 年 7 月)all current browsers except for IE11 将用户时区作为字符串返回。

    【讨论】:

    • 在 Firefox 中返回 undefinedlet timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
    • let 任何东西都会返回undefined,你需要评估timezone,或者只运行Intl.DateTimeFormat().resolvedOptions().timeZone
    【解决方案5】:

    投票最多的answer 可能是获取时区的最佳方式,但是,Intl.DateTimeFormat().resolvedOptions().timeZone 根据定义返回 IANA 时区名称,即英文。

    如果您想要当前用户语言的时区名称,您可以从Date 的字符串表示中解析它,如下所示:

    function getTimezoneName() {
      const today = new Date();
      const short = today.toLocaleDateString(undefined);
      const full = today.toLocaleDateString(undefined, { timeZoneName: 'long' });
    
      // Trying to remove date from the string in a locale-agnostic way
      const shortIndex = full.indexOf(short);
      if (shortIndex >= 0) {
        const trimmed = full.substring(0, shortIndex) + full.substring(shortIndex + short.length);
        
        // by this time `trimmed` should be the timezone's name with some punctuation -
        // trim it from both sides
        return trimmed.replace(/^[\s,.\-:;]+|[\s,.\-:;]+$/g, '');
    
      } else {
        // in some magic case when short representation of date is not present in the long one, just return the long one as a fallback, since it should contain the timezone's name
        return full;
      }
    }
    
    console.log(getTimezoneName());

    在 Chrome 和 Firefox 中测试。

    当然,这在某些环境中不会按预期工作。例如,node.js 返回 GMT 偏移量(例如 GMT+07:00)而不是名称。但我认为它仍然可以作为后备阅读。

    附:在 IE11 中不起作用,就像 Intl... 解决方案一样。

    【讨论】:

    • 这是一个很好的解决方案。我正在使用timeZoneName: 'short' 来获取可以在 Java 后端使用的缩写形式,例如 EST 或 GMT+0230。
    【解决方案6】:

    按名称检索时区(即“美国/纽约”)

    moment.tz.guess();
    

    【讨论】:

    • 请提一下,它也可以在没有任何依赖关系的情况下简单地实现:Intl.DateTimeFormat().resolvedOptions().timeZone
    • 是的,尽管 moment-js 也猜测 TZ 是不支持该功能的“浏览器”。 :)
    • 在 IE11 Intl.DateTimeFormat().resolvedOptions().timeZone 中返回 undefined,但 moment.tz.guess() 返回正确的值
    【解决方案7】:

    这将获取旧 javascript 中的时区代码(例如,GMT)(我正在使用带有旧引擎的谷歌应用脚​​本):

    function getTimezoneName() {
      return new Date().toString().get(/\((.+)\)/);
    }
    

    我只是把它放在这里以防有人需要它。

    【讨论】:

    • 我认为这只适用于UTC/GMT。看起来其他人都被拼写出来了;例如,Pacific Daylight Time.
    【解决方案8】:

    当前用户语言的结果的可能性很小:

    console.log(new Date().toLocaleDateString(undefined, {day:'2-digit',timeZoneName: 'long' }).substring(4));

    【讨论】:

      【解决方案9】:

      要检测“美国/纽约”之类的内容,您可以使用Luxon library 中的new LocalZone()

      import { LocalZone } from 'luxon';
      
      const zoneName = new LocalZone().name;
      

      【讨论】:

        猜你喜欢
        • 2021-07-24
        • 1970-01-01
        • 2023-02-21
        • 1970-01-01
        • 2013-08-17
        • 2021-11-28
        • 2013-03-03
        • 2021-04-11
        • 1970-01-01
        相关资源
        最近更新 更多