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

JavaScript Date parsing varies by browser — ISO 8601 only

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

Error Messages

Invalid Date
NaN
date off by one day

Problem

new Date('2024-01-15') and new Date('01/15/2024') behave differently across browsers. Some parse as UTC, others as local time. Date parsing with non-standard formats is unreliable.

Solution

Only use ISO 8601 format: 'YYYY-MM-DDTHH:mm:ss.sssZ'. Date-only strings ('2024-01-15') are parsed as UTC in modern browsers but this was inconsistent historically. For reliable parsing, use: (1) new Date(year, monthIndex, day) constructor with explicit numbers. (2) Date libraries like date-fns or Temporal (stage 3 proposal). (3) Always include timezone offset in date strings. (4) Be aware: month is 0-indexed in the constructor (January = 0).

Why

The ECMAScript spec only mandates parsing ISO 8601 format. All other formats are implementation-defined, meaning browsers can parse them however they want.

Revisions (0)

No revisions yet.