zloirock/core-js

ES6 Date parsing

开放

#63 创建于 2015年5月2日

 (5 条评论) (0 个反应) (0 位负责人)JavaScript (1,609 个派生)batch import
enhancementeshelp wanted

仓库指标

星标
 (23,771 个星标)
PR 合并指标
 (PR 指标待抓取)

描述

The behavior or parsing a date or date+time string to a Date object (using the new Date(string) constructor, or Date.parse(string) function) is changed in ES6. AFAIK, this isn't currently handled by the core-js ES6 polyfills. Should it be? My understanding is that this is a dependency for libraries such as Babel. (originally filed as babel/babel#1418)

Behavior

ES6 §20.3.1.16

If the time zone offset is absent, the date-time is interpreted as a local time.

ES5.1 §15.9.1.15:

The value of an absent time zone offset is "Z".

To get ES6 behavior out of ES5, the string would need to be mutated to be interpreted in local time. The polyfill would have to be careful only to do this for the specific use cases affected.

ES6 ES5
new Date("2014-12-31") new Date("2014/12/31")
new Date("2014-12-31T01:23:45") new Date("2014/12/31 01:23:45")
new Date("2014-12-31T01:23:45Z") no change
new Date("2014-12-31T01:23:45-05:00") no change

Note, the hyphens are replaced with slashes, but still kept in year/month/day order to prevent internationalization ambiguities. Also note the T removed. Also note that ECMAScript only defines the ISO8601/RFC3339 format, leaving everything else implementation specific. It just happens that all major implementations support the formats I showed.

贡献者指南