更改计算机的时区后,Chrome无法正确获取时区名称 [英] Chrome cannot get TimeZone name correctly after changing the TimeZone of computer

查看:113
本文介绍了更改计算机的时区后,Chrome无法正确获取时区名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在javascript中有如下代码段:

I have a snippet code in javascript as shown below:

$(document).ready(function () {    
    var day1 = new Date();
});

现在我打开浏览器(在这种情况下,我使用FireFox),并且我计算机的当前时区为 UTC +10:00霍巴特。然后,我在调试模式下查看 day1 javascript变量,该变量将其值显示如下图所示。

Now I open a browser (in this case I use FireFox) and the current TimeZone of my computer is UTC +10:00 Hobart. Then I view the day1 javascript variable in debug mode, it displays its value as image shown below.

UTC + 10:00霍巴特的时区名称为塔斯马尼亚标准时间

接下来,我将计算机上的TimeZone更改为 UTC + 10:00堪培拉,墨尔本,悉尼(打开 FF浏览器)。然后,我按Ctrl + F5刷新FF浏览器,并再次在调试模式下查看 day1 的值。

Next, I will chang the TimeZone on my computer to UTC+10:00 Canberra, Melbourne, Sydney while FF browser is being opened. Then I refresh FF browser by pressing Ctrl+F5 and view day1's value in debug mode again.

如您所见,FF将新的正确时区名称更新为 AUS东部标准时间

As you can see, FF updated the new correct TimeZone name as AUS Eastern Standard Time. It worked as my expected too.

MS Edge和IE与FF的工作方式相同。

MS Edge and IE worked as the same way with FF.

但是,Chrome以不同的方式工作。

Chrome返回给我的新TimeZone名称是 Local Daylight Time ,而不是我期望的和其他浏览器(FF,MS Edge,IE)一样的AUS Eastern Standard Time 。

The new TimeZone name that Chrome returns to me is Local Daylight Time, not AUS Eastern Standard Time as my expected and as the other browsers (FF, MS Edge, IE) did.

Chrome为什么不返回 AUS Eastern Standard Time名称?

Why does not Chrome return "AUS Eastern Standard Time" name?

有什么方法可以解决吗?

Is there any way can solve this case for Chrome?

推荐答案

不幸的是,时区名称的实现未标准化。 ECMAScript规范允许它成为任何供应商想要的东西。不仅在浏览器之间不同,而且在浏览器的不同版本之间以及在操作系统之间也不同。

Unfortunately, the implementation of time zone name is not standardized. The ECMAScript spec allows it to be anything the vendor wants it to be. It's not even just different across browsers, but also across different versions of the browser, and across operating systems.


来自 ECMA-262(5.1)15.9.5.2节(强调我的)

Date.prototype.toString()

此函数返回一个String值。 字符串的内容取决于实现方式,但旨在以一种方便易懂的形式表示当前时区中的日期。

This function returns a String value. The contents of the String are implementation-dependent, but are intended to represent the Date in the current time zone in a convenient, human-readable form.

如果您要尝试检测用户的时区,则有一个更新的标准可以检索时区名称,作为IANA时区标识符(例如, Australia / Sydney ),您可以这样操作:

If what you're trying to do is to detect the user's time zone, there is a newer standard for retrieving the time zone name, as an IANA time zone identifier (ex, Australia/Sydney), which you can do like this:

Intl.DateTimeFormat().resolvedOptions().timeZone

但是,并非所有浏览器都已实现此功能。有些库可以对此进行猜测,您可以在此处阅读

However, not all browsers have implemented this yet. There are libraries that can guess at it though, which you can read about here.

这篇关于更改计算机的时区后,Chrome无法正确获取时区名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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