chrome 67 date vs Chrome 66 date

2019-08-25 12:44发布

The code is:

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

In Chrome 66 it returns:

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

In Chrome 67 it returns:

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

Why the difference?

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

1条回答
霸刀☆藐视天下
2楼-- · 2019-08-25 13:17

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:

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.

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.

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.

查看更多
登录 后发表回答