使用 Moment's guess() 返回时区名称 [英] Return timezone name with Moment's guess()

查看:215
本文介绍了使用 Moment's guess() 返回时区名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Moment 的 guess() 函数 返回时区大陆(或国家)和城市,如他们的例子:

I want to use Moment's guess() function to return the timezone continent (or country) and city as in their example:

moment.tz.guess(); // America/Chicago

但是上面的方法不起作用.返回值是CET,这是别的东西.它是中欧时间的缩写,即没有大陆国家,没有城市.

But the above doesn't work. The returned value is CET, which is something else. It's an abbreviation of the Central European Time, i.e. no continent-country, no city.

如何使用 guess() 函数返回欧洲/斯德哥尔摩"、美国/芝加哥"等?

How do I use the guess() function to return 'Europe/Stockholm', 'America/Chicago' etc?

推荐答案

当宿主环境(浏览器等)实现了 ECMA-402 的 API 但在结果中返回了错误的时区类型时,就会发生这种情况.如果您使用 Moment 看到这一点,您可能也可以通过以下方式在没有 Moment 的情况下获得此结果:

This happens when the host environment (browser, etc.) implements the API for ECMA-402 but returns the wrong type of time zone in the result. If you are seeing this with Moment, you can probably also get this result without Moment with the following:

Intl.DateTimeFormat().resolvedOptions().timeZone

目前,Moment 假设如果此 API 存在,则它要么提供有效的 IANA 时区标识符,要么返回 undefined.但是,如您所指出的,某些环境会返回诸如 CET 之类的内容.

Presently, Moment assumes that if this API exists, that it either provides a valid IANA time zone identifier or returns undefined. However, some environments return things like CET as you pointed out.

忽略不良结果已记录为 moment/moment-timezone#423 中的功能请求.在这种特殊情况下,环境是 Android 设备上的旧版 Chrome 浏览器.

Ignoring bad results is already recorded as a feature request in moment/moment-timezone#423. In that particular case, the environment was an older Chrome browser on an Android device.

这篇关于使用 Moment's guess() 返回时区名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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