Safari invalid date, JavaScript new date return “Invalid Date”

A JavaScript bug only in Safari. Chrome and Firefox work well but it turns out you always need to do browser testing, and Safari is always full of surprises!

The pattern yyyy-MM-dd isn’t an officially supported format for Date constructor

Replace dash by slashes and it should be an easy fix!

console.log (new Date('2022-04-10'.replace(/-/g, "/")));

Safari doesn’t like the – but the same format with a / work

You could look at libraries such as DateJS for parsing non-standard date formats, but maybe you don’t need it! Less is best.

Notify of

Inline Feedbacks
View all comments