zloirock/core-js

ES6 Date parsing

Open

#63 opened on May 2, 2015

View on GitHub
 (5 comments) (0 reactions) (0 assignees)JavaScript (23,771 stars) (1,609 forks)batch import
enhancementeshelp wanted

Description

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.

Contributor guide