GGistDev

Dates and Intl in JavaScript

Work with dates/times and internationalization features for numbers, dates, and messages.

Date basics

const now = new Date()
const fromTs = new Date(1700000000000)
const fromIso = new Date('2024-01-01T12:00:00Z')
now.getFullYear(); now.getMonth(); now.getDate()

Date is mutable and represents an absolute time internally (ms since epoch). Months are zero‑based.

Formatting dates

Use Intl.DateTimeFormat for locale‑aware formatting.

new Intl.DateTimeFormat('en-US', { dateStyle: 'medium' }).format(now)
new Intl.DateTimeFormat('de-DE', { timeStyle: 'short' }).format(now)

Parsing and time zones

  • Prefer ISO 8601 strings for interchange
  • Use libraries or Temporal (proposal) for robust time zone handling

Relative time

new Intl.RelativeTimeFormat('en').format(-1, 'day') // '1 day ago'

Number and currency formatting

new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD' }).format(1234.56)

Plural rules

new Intl.PluralRules('en').select(2) // 'other'

Collation (string compare)

new Intl.Collator('en').compare('a', 'b')

Temporal (proposal)

Temporal provides modern date/time types (PlainDateTime, ZonedDateTime). Use polyfills until widely supported.

Summary

  • Use Intl.* formatters for locale‑aware output
  • Prefer ISO strings; beware Date quirks and time zones
  • Watch for Temporal to replace many Date pitfalls