📎 Webclip
Temporal: getting started with JavaScript’s new date time API
Temporal is presented as a newer and easier date time API for JavaScript than Date. The post focuses on how Temporal separates exact time from wall-clock time, supports time zones and non-Gregorian calendars, and uses specialized immutable classes for different kinds of temporal values.
Reading notes#
- Temporal is accessed through the global
Temporalobject and uses immutable values. - The API supports time zones and non-Gregorian calendars.
- Different classes represent different kinds of time values, which makes the context of a value easier to understand.
Instantrepresents exact UTC time and is mainly a container for epoch nanoseconds.ZonedDateTimecombines wall-clock time, a time zone, and a calendar.PlainDateTime,PlainDate, andPlainTimeare timezone-less, abstract representations of time.PlainYearMonthis used for a month in a year.PlainMonthDayis used for a day in a month.Temporal.nowprovides the current time in several forms, includinginstant(),zonedDateTimeISO(),plainDateTimeISO(), andplainTimeISO().- Temporal time zones are based on the IANA Time Zone Database.
- The post shows that the same time zone can have different offsets during standard time and daylight saving time.
- Temporal calendars are based on Unicode CLDR and include
iso8601,gregory,japanese,islamic, and others. - Temporal adds support for date time strings that can include IANA time zone names and calendar systems.
.from()accepts strings, same-class instances, and field objects..toString()can be configured with options such asoffset,timeZoneName, andsmallestUnit.- Temporal values can be serialized to JSON with
.toJSON(). - Temporal supports human-readable output through
.toLocaleString(), similar toIntl.DateTimeFormat. - Legacy
Datevalues can be converted toTemporal.Instant, andInstantcan be converted back through epoch milliseconds. - The post shows how to read field values, create modified copies with
.with(), and sort values withD.compare. - Temporal values can be converted between
Instant,ZonedDateTime, andPlainDateTime. - Temporal values can also be converted between time zones with
.withTimeZone(). - Duration arithmetic is used for differences and additions, including examples for flight time, next day computation, and finding the first Monday in September.
- The post notes that the proposal is not ready for unflagged shipping and that the polyfill is useful for experimentation but not for production.
