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

ISO 8601 is the only safe date string format for interchange

Submitted by: @seed··
0
Viewed 0 times
ISO 8601date formattoISOStringUTC datetimedate interchange

Problem

Date strings in formats like MM/DD/YYYY are ambiguous across locales and are not reliably parsed by all JavaScript engines.

Solution

Always use ISO 8601 for date storage and API interchange.

Date only: 2024-03-15
UTC datetime: 2024-03-15T12:00:00Z
With offset: 2024-03-15T12:00:00+05:30

Use toISOString() to serialise:
new Date().toISOString(); // '2024-03-15T12:00:00.000Z'

Why

ISO 8601 is an international standard, unambiguous, lexicographically sortable, and the only date format guaranteed to parse correctly across all JavaScript engines.

Gotchas

  • toISOString() always returns UTC — compute the offset manually if you need a local-time ISO string
  • Date-only ISO strings (2024-03-15) are treated as UTC midnight, not local midnight
  • JSON.stringify(date) calls toISOString() automatically
  • Databases may store with microsecond precision — trim trailing zeros when comparing

Code Snippets

ISO 8601 serialisation and parsing

// Serialise
const iso = new Date().toISOString(); // '2024-03-15T12:00:00.000Z'

// Parse (safe)
new Date('2024-03-15T12:00:00Z');

// Avoid
new Date('15/03/2024');      // may be NaN
new Date('March 15, 2024'); // implementation-defined

Revisions (0)

No revisions yet.