chrome 67 date vs Chrome 66 date [英] chrome 67 date vs Chrome 66 date

查看:43
本文介绍了chrome 67 date vs Chrome 66 date的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码是:

var rightNow = new Date();
console.log(rightNow);

在Chrome 66中返回:

Tue Jun 12 2018 15:36:19 GMT+0530 (IST)

在Chrome 67中返回:

Tue Jun 12 2018 15:36:43 GMT+0530 (India Standard Time)

为什么会有差异?

我的很多代码都适用于chrome 66中的行为。我是否需要更改所有代码?

A lot of my code works with the behaviour in chrome 66. Do I need to change all the code?

推荐答案

通常,您不应为特定的浏览器编码,而应遵循标准。在这种情况下,标准为ECMAScript(ECMA-262)。 第20.3.4.41节涵盖了 Date.prototype.toString(),这是指以下部分描述内部 ToDateString(tv)函数的内容,这说明:

In general, you should not code for a specific browser, but to the standards. In this case, the standard is ECMAScript (ECMA-262). Section 20.3.4.41 covers Date.prototype.toString(), which refers to the following section describing the internal ToDateString(tv) function, which explains:


使用方便,人工的方式返回实现相关的String值,该值将tv表示为当前时区中的日期和时间。可读形式。

Return an implementation-dependent String value that represents tv as a date and time in the current time zone using a convenient, human-readable form.

通过实现依赖,这意味着实际的字符串值不是由规范定义的,并且可以从一种实现到另一种实现。不能保证从一个浏览器到下一个浏览器,或者从一个浏览器的一个版本到下一个,甚至从一个操作系统从同一浏览器的相同版本到下一个,都无法保证得到相同的结果。

By "implementation-dependent", it means that the actual string value is not defined by the specification, and can vary from one implementation to another. You are not guaranteed the same result from one browser to the next, or from one version of a browser to the next, or even from one operating system to the next from the same version of the same browser.

用人类可读的形式表示所产生的值适合于向人类展示。它不能保证该值以可以被计算机代码一致地解析的方式表示。

By "human-readable form", it means that the value produced is suitable for display to a human being. It makes no guarantees about that value being represented in a manner that can be parsed consistently by computer code.

因此,如果您打算将字符串值发送给其他人代码,则不应使用 .toString()。通常,出于此目的,您应该首选ISO 8601格式的字符串。如果要使用UTC表示结果,请使用 .toISOString()。如果您希望以本地时间(包括时区偏移量)来获取结果,请参考此答案(或使用库)。

Thus, if you intend for the string value to be sent to other code, you should not use .toString(). In general, you should prefer an ISO 8601 formatted string for this purpose. Use .toISOString() if you want the result in terms of UTC. Refer to this answer (or use a library) if you want the result in terms of local time including a time zone offset.

关于Chrome 66和Chrome 67之间为何发生变化的原因-我没有确切的详细信息,但是我认为Chrome不再使用 IANA TZDB 的缩写,表示使用 CLDR 时区名称,可能是通过使用 ICU 来实现的。这是合理的,并且是许多其他实现方式正在做的事情。并不需要使用一组数据,也不要使用其他数据。

As to why things changed between Chrome 66 and Chrome 67 - I don't have the exact details, but I assume Chrome switched from using IANA TZDB abbreviations to using CLDR time zone names, probably through its use of ICU. This is reasonable, and is what many other implementations are doing. There's no requirement it use one set of data or the other though, so don't rely on such things.

这篇关于chrome 67 date vs Chrome 66 date的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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