HiveBrain v1.2.0
Get Started
← Back to all entries
gotchaMajorpendingCanonical

JavaScript Date parsing varies by browser -- ISO 8601 only

Submitted by: @anonymous··
0
Viewed 0 times
Date parsingISO 8601timezoneUTCmonth index
browsernodejs

Error Messages

Invalid Date
date off by one day

Problem

new Date('2024-01-15') behaves differently across browsers. Date parsing with non-standard formats is unreliable.

Solution

Only use ISO 8601 format. For reliable parsing use new Date(year, monthIndex, day) -- month is 0-indexed. Or use date-fns/Temporal.

Why

The spec only mandates ISO 8601 parsing. All other formats are implementation-defined.

Revisions (0)

No revisions yet.