Work is underway to implement 'Temporal,' which will greatly simplify JavaScript date and time management



JavaScript has an object called '

Date ' for representing time, but it has problems such as difficulty in handling time zones and unintuitive API. A new object for displaying time, ' Temporal ', has appeared to solve these problems, and work is underway to implement it in browsers.

JavaScript Temporal is coming | MDN Blog
https://developer.mozilla.org/en-US/blog/javascript-temporal-is-coming/



The JavaScript Date object was created by copying the implementation of Java's 'java.util.Date' when JavaScript was created in 1995, and the same API has been used for 30 years. Although Date supports the user's local time and UTC (Coordinated Universal Time), it is difficult to handle other time zones, and the time parsing operation is not very reliable. For this reason, many developers used dedicated libraries such as

Moment.js and date-fns to properly handle dates and times.

The new Temporal object is designed to be a drop-in replacement for the Date object, supporting many built-in methods for time zone and calendar representation, conversion, comparison and arithmetic, formatting, and more.

To support these powerful functions, Temporal has multiple classes and over 200 methods at the time of writing. Although it may seem complicated at first glance, Temporal's classes can be broadly divided into the following three categories:

◆1: Expresses a period
Temporal.Duration is an object that indicates the difference between two points in time. It is displayed in terms of date, month, year, and time, just like a normal time, but it can take negative values.

◆2: Identify a moment in history
Temporal.ZonedDateTime includes time zone information in the date and time, so it can indicate a point in history. If you need a simple timestamp, you can use Temporal.Instant .

◆3: Expressing date and time
Temporal.PlainDateTime allows you to represent a complete time, including the date, hour, minute, second, millisecond, and nanosecond. Adding time zone information to PlainDateTime is equivalent to ZonedDateTime.

Additionally, the ' Temporal.Now ' object provides a number of methods to obtain the current time in various formats.



In addition, Temporal has fixed a confusing API specification. When setting or displaying the month in Date, January was set to '0' and December to '11', but in Temporal, January can be displayed and set to '1' and December to '12' as they appear. In addition, since all Temporal objects are immutable, bugs due to unexpected side effects no longer occur.

At the time of writing, Temporal's implementation in browsers is underway. The progress of the implementation work in the three major browsers, Chrome, Firefox, and Safari, can be checked on the following page.

Chrome

Implement the Temporal proposal [42201538] - Chromium

・Firefox
1912757 - Build temporal in Nightly by default

・Safari
223166 – [JSC] Implement Temporal

in Software, Posted by log1d_ts