An Introduction to the JavaScript Temporal API

An Introduction to JavaScript Temporal API

Whatever your opinion of JavaScript, everyone thinks date handling is a mess. The Date() object implementation was copied directly from Java in 1995. Java scrapped it two years later but it remained in JavaScript for backward compatibility.

These are the problems we face with Date():

  • it only supports UTC and the user’s local time
  • there’s no support for non-Gregorian calendars
  • daylight-saving behavior is unpredictable
  • parsing dates from strings is unreliable
  • the API is clunky
  • Date objects are mutable: a date will change as methods are applied

The most popular alternative used to be the moment.js date library. However, this has been placed in maintenance mode and should not really be used in new projects. They recommend a couple of alternatives, but each of these have their own pros and cons.

The Date() object can’t be removed from JavaScript, but a new Temporal option is at stage 2 in the TC39 standards approval process. The properties and methods discussed here are subject to change, but the API should arrive in browsers and runtimes some time in 2021.

Temporal Time

Temporal is a top-level static global object (like Math).

Its main objectives are:

  • a predictable cross-browser/runtime behavior
  • easier APIs for date and time computations
  • support for non-Gregorian calendars
  • support for all time zones, including daylight-saving arithmetic
  • parsing of strictly specified ISO-8601 strings
  • making all objects immutable

The API is comprehensive and may change, but you can look forward to the following key features at some point in the near future.

Current Date and Time

Temporal.now returns the current date/time, which can be passed to further methods to provide additional information. For example:

// exact time since the Unix epoch on 1 Janary, 1970 UTC Temporal.now.instant(); Temporal.now.instant().epochSeconds; Temporal.now.instant().epochMilliseconds; // current time zone Temporal.now.timeZone(); // time in current location, e.g. // 2021-09-18T04:17:48.435068431-04:00[America/New_York] Temporal.now.zonedDateTimeISO(); // time in another time zone, e.g. // 2021-09-18T09:17:48.438068435+01:00[Europe/London] Temporal.now.zonedDateTimeISO('Europe/London'); 

Creating Instant Date/Times

The Temporal.Instant object represents a single point in time to the nearest nanosecond. It can be created from ISO 8601 formatted strings, or a number of seconds, milliseconds, or microseconds:

const t1 = Temporal.Instant.from('2021-03-30T01:45:00+01:00[Europe/Berlin]'), t2 = Temporal.Instant.from('2021-04-01T02:00+01:00'), t3 = Temporal.Instant.fromEpochSeconds(1.0e8), t4 = Temporal.Instant.fromEpochMilliseconds(1.0e10), t5 = Temporal.Instant.epochNanoseconds(1.0e12); 

Creating Zone-aware Date/Times

The Temporal.ZonedDateTime object represents a timezone and calendar-aware date/time at the instant it occurred (or will occur) in a particular region. A variety of different constructors can be used:

new Temporal.ZonedDateTime( 1234567890000, // epoch nanoseconds Temporal.TimeZone.from('America/Los_Angeles'), // timezone Temporal.Calendar.from('iso8601') // default calendar ); Temporal.ZonedDateTime.from('2025-12-07T03:24:30+02:00[Africa/Cairo]'); Temporal.Instant('2022-08-05T20:06:13+05:45').toZonedDateTime('+05:45'); Temporal.ZonedDateTime.from({ timeZone: 'America/New_York' year: 2025, month: 1, day: 7, hour: 9, minute: 30, second: 1, millisecond: 2, microsecond: 3, nanosecond: 4 }); 

Continue reading An Introduction to the JavaScript Temporal API on SitePoint.