`new Date` does not work with a valid ISO-8601 date string
See original GitHub issueI’m using the react-intl
library which relies on new Date()
to function for date formatting. Storybook injects a few ES5 shims from es5-shim
(from airbnb-js-shims
), which overwrites the browsers Date
function with its own. Using es5-shim
’s Date
function, it cannot parse this valid ISO 8601 date string: “2017-04-31T10:39:10-04:00”, and returns Invalid date
. Unfortunately, we’re using Storybook to test these components which heavily rely on dates and we’re constantly seeing Invalid date
.
In the following, you’ll notice the same date will return different results on a vanilla Chrome instance with and without Storybook
Expected (vanilla Chrome)
new Date('2017-04-31T10:39:10-04:00')
// Mon May 01 2017 10:39:10 GMT-0400 (EDT)
typeof new Date('2017-04-31T10:39:10-04:00')
// "object"
Date('2017-04-31T10:39:10-04:00')
// "Tue Aug 23 2016 16:07:38 GMT-0400 (EDT)"
typeof Date('2017-04-31T10:39:10-04:00')
"string"
Currently (vanilla Chrome with Storybook)
new Date('2017-04-31T10:39:10-04:00')
// Invalid Date
typeof new Date('2017-04-31T10:39:10-04:00')
// "object"
Date('2017-04-31T10:39:10-04:00')
// "Tue Aug 23 2016 16:09:07 GMT-0400 (EDT)"
typeof Date('2017-04-31T10:39:10-04:00')
"string"
Issue Analytics
- State:
- Created 7 years ago
- Comments:7 (6 by maintainers)
Top Results From Across the Web
New Date from ISO-8601 throws 'Invalid Date' [duplicate]
I have two dates both in a valid ISO-8601 format and I'm trying to convert them to a Date object. console.log(new Date('2019-08-03T18:17: ...
Read more >new Date does not work with a valid ISO-8601 date string #400
Using es5-shim 's Date function, it cannot parse this valid ISO 8601 date string: "2017-04-31T10:39:10-04:00", and returns Invalid date .
Read more >ISO8601DateFormatter fails to parse a valid ISO-8601 date
ISO8601DateFormatter does not support full ISO08601 with regard to fractional seconds. The way I've worked around it is by using a .custom ....
Read more >Date() constructor - JavaScript - MDN Web Docs - Mozilla
The Date() constructor can create a Date instance or return a string representing the current time.
Read more >ISO 8601: The global standard for date and time formats
ISO 8601 solves precisely this problem. ... SSSZ"); try { Date unformatedDate = format.parse(date); String formatedDate = new ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@ljharb That’s due to sudden of the release of Storybook 2. I think it was too quick. Seems like it’s only Chrome thing. I’ll revert this commit.
This is still happening with this versions
Storybook - ^6.0.28 Chrome - Version 86.0.4240.193
Error log in Chrome
Uncaught TypeError: Cannot read property '2' of undefined at new ShiftedDate (<anonymous>:48:42) at new Date (vendors~main.39af3481098ad3d1e989.bundle.js:117388) at Object.<anonymous> (vendors~main.39af3481098ad3d1e989.bundle.js:121814) at vendors~main.39af3481098ad3d1e989.bundle.js:118044 at Object.<anonymous> (vendors~main.39af3481098ad3d1e989.bundle.js:118048) at Object../node_modules/es6-shim/es6-shim.js (vendors~main.39af3481098ad3d1e989.bundle.js:121911) at __webpack_require__ (runtime~main.39af3481098ad3d1e989.bundle.js:849) at fn (runtime~main.39af3481098ad3d1e989.bundle.js:151) at Object../node_modules/airbnb-js-shims/target/es2015.js (vendors~main.39af3481098ad3d1e989.bundle.js:43725) at __webpack_require__ (runtime~main.39af3481098ad3d1e989.bundle.js:849)
Firefox and Safari working as expected.