ES Temporal: 2 Minute Standards
You might have heard that Temporal
has recently reached Stage 3
in ECMA, here's a
#StandardsIn2Min explaination of it...
Since it's beginnings JavaScript has had only a rudimentary
Date
object. It copied Date
from an early edition
of Java and it was intended to be used for programming just about anything
relating to time. While Java itself quickly deprecated and improved their
situation, JavaScript implementations didn't follow suit. It also introduced
its own warts and quirks to Date
along the way. As a result,
JavaScript libraries dedicated toward reasoning about the complexities of
time, like moment.js
became common and essential.
Temporal
, now at stage 3 as of this writing, is the result of a
lot of work initiated by maintainers of those projects and shepherded
through the standards process. It introduces lots of rich APIs to the
JavaScript standard library, hosted by a new top-level
Temporal
object. Large top-level introductions of this sort
(like Temporal, Math and Intl) are exceedingly rare.
Temporal
provides a number of objects, all immutable and
serializable, and each with their own methods for reasoning about time in
different ways.
It contains some fundamental concepts which implement standards for calendar systems and timezones respectively, and set the foundations for how a lot of methods do their work:
Temporal.Calendar
Temporal.TimeZone
It introduces Temporal.Instant
which is used for dealing with
an instant in time to various degrees of precision.
It also introduces a number of "plain" themed objects geared toward providing APIs for the different ways we think about time not simply in terms of different kinds of precision. For example...
-
Temporal.PlainTime
is for dealing with wall-clock time that is not associated with a particular date or time zone. -
Temporal.PlainMonthDay
is for date without a year components, useful for annual events like "The fourth of July" or "Christmas Day". -
Temporal.PlainDateTime
is for representing a calendar date and wall-clock time that does not carry time zone information, e.g. December 7th, 1995 at 3:00 PM (in the Gregorian calendar). -
Temporal.PlainYearMonth
is useful for expressing things like "The October 2020 edition of Vanity Fair".
It also includes...
-
Temporal.ZonedDateTime
is for reasoning about dates and times in the timezone offsets reckoned by a particular calendar -
Temporal.Duration
is used for measuring the duration between two temporal objects. -
Temporal.now
provides APIs about current the moment in time.
You can learn a lot more by following through links in the Temporal
proposal repo, including helpful
Reference documentation and
examples, a
cookbook to help you
get started and learn the ins and outs of Temporal which include a (not
production ready) polyfill in every page so that you can open Dev Tools and
explore for yourself.
If you're interested in hearing about the history, development, challenges, inner workings or rationale behind any of this, I recently hosted an edition of our podcast on this topic with guests who worked on the standard.