question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`new Date` does not work with a valid ISO-8601 date string

See original GitHub issue

I’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:closed
  • Created 7 years ago
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
arunodacommented, Sep 3, 2016

@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.

0reactions
rnananiacommented, Nov 10, 2020

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found