Home DEVELOPER JavaScript Temporal API: New era for date and time calculation

JavaScript Temporal API: New era for date and time calculation

0


Something is happening in Temporal API, the interface that is considered to cure one of the biggest weaknesses in JavaScript programming language: dealing with date and time. TC39, the standardization committee for JavaScript has been working on Temporal API for a long time, which has been considered to replace the date interface. Date interface problems are diverse, there is a lack of support for discrepancies, defects and time areas. Temporal API should not only change the date API, but also record it with installed libraries such as date-FNS, day. JS or Luxon.

Advertisement





The sebastian works as a JavaScript engineer in Springer Mabornavolf. In addition to the development and concept of applications, their focus is on the performance of knowledge. As a lecturer for JavaScript, the spokesperson in many conferences and author, he tries to awaken the enthusiasm for professional development with JavaScript.

TC39 has implemented a polyphil- ie. However, it is clearly not intended for productive use. Meanwhile, there are two packages with @js temporal/polyfils and temporal poyfils, which can be used instead. With around 160,000 and 130,000 weekly NPM downloads, its distribution cannot be compared with a 26 million date FNS or 22 million days. However, the situation will change immediately.

A new AI of China sees itself as an artificial general intelligence

Firefox is the first browser to give full implementation of Temporal API in the construction of the browser, which means the first step to integrate a new interface. In its technical preview, the safari provides individual parts of APIs and all other browser are also actively working on the integration of the new API.

At the moment, the interface can only be used through polyphil package, but it is on the way to become a new JavaScript standard to work over the date and time.

Temporal API not only addresses the inadequacy of the date API of JavaSkcript, but also takes many useful functional from the established date libraries. In addition, the interface creates on modern architectural patterns. Temporal’s focus is:

  • Stability: The temporal has a series of classes that provide an uniform and intuitive interface to work with date and time.
  • Strong: The interface supports time zone and various calendar systems such as ISO 8601 calendar, Indian calendar, traditional Korean calendar and many others.
  • Inaccuity: Temporary items are not variable. Methods that manipulate the date or time, such as joint or subtraction, produce new examples and do not change the original object. This prevents side effects that can arise from the manipulation of references.
  • accuracy: API works with accuracy of nanoseconds and is not like a dated object with milliseconds. In doing so, it supports the highly accurate time tickets, as it uses, for example, hooks API from the performance node.J.

Temporal API provides several classes for various applications during processing date and time values:

  • PlainMonthDay: Regardless of the year, stands for a day and month. It can be used for recurring events such as birthdays.
  • PlainYearMonth: This class represents a month in a certain year without one day, time or time area. A potential application is a monthly billing period, for example cell phone bill for March 2025.
  • PlainDate, PlainDate Time and time stands for a certain day without field. An application is a person’s birthday, for example 04.07.1961 (Brandon Birthday – Inventor of JavaScript).
  • PlainTime: Any day stands for a time. There is no reference to any date or time area. A specific case is the beginning of a daily recurring appointment like lunch brake.
  • PlainDateTime: It is date and time, so it stands for a time on a certain day. An example of this class can represent the total solar eclipse on August 11, 1999 at 12:35 pm (in Munich).
  • Instant: One Instant-Intensity is a certain point in the time that has been shown as several nanoskands since January 1, 1970. Hence the time area has been fixed on the UTC. Objects of this class can be used, for example, to measure the time between two events.
  • ZonedDateTime: This class is best configured. This includes date, time and time area. An example of this type is the beginning of a cross -zone meeting.


Temporal API classes

Temporal API classes

(Picture: TC39,

Based on the application, there are many ways to create a new temporary example. Listing shows it using an example of 1 square PlainDateTime,

const now = Temporal.Now.plainDateTimeISO();
console.log(now.toString()); // 2025-02-23T11:06:41.898

const newDateTime = new Temporal.PlainDateTime(1999, 8, 11, 12, 35);
console.log(newDateTime.toString()); // 1999-08-11T12:35:00

const newDateTime1 = Temporal.PlainDateTime.from(newDateTime);
console.log(newDateTime1.toString()); // 1999-08-11T12:35:00

const newDateTime2 = Temporal.PlainDateTime.from('1999-08-11T12:35');
console.log(newDateTime2.toString()); // 1999-08-11T12:35:00

const newDateTime3 = Temporal.PlainDateTime.from({
  year: 1999,
  month: 8,
  day: 11,
  hour: 12,
  minute: 35,
});
console.log(newDateTime3.toString()); // 1999-08-11T12:35:00

const newDate = new Date('1999-08-11T12:35');
const instant = newDate.toTemporalInstant();
const zoned = instant.toZonedDateTimeISO('Europe/Berlin');
const newDateTime4 = zoned.toPlainDateTime();
console.log(newDateTime4.toString()); // 1999-08-11T12:35:00

Listing 1: Creating new examples of class PlainDateTime

For the present time, Temporal API sees object Temporal.Now With many ways. Method plainDateTimeISO Makes PlainDateTime-object.

There are separate variants for such examples that stand for a specified time. First goes to a class constructor PlainDateTimeIt accepts several parameters that stand for the individual parts of the time. Years, months and days are compulsory. By default, all further information is value 0. Quite more flexible and therefore static method is recommended fromShe either accepts one PlainDateTime-Oct where he makes a copy, an ISO string that represents the date, or an object with information about the date as property values.

Slightly more cumbersome, but still possible, converting a JavaScriptDate-This PlainDateTime-object. Date API will be the law in future toTemporalInstant provide. Instant-If it applies to one ZoneDateTime To convert, which in the end one PlainDateTime-S object can be converted. It is possible to integrate cosmic APIs in existing applications.

Creating date objects was already possible with previous means of JavaScript, in an inconsistent way. Are really interesting

Extension are new operations that make temporal API possible.

New models of smart lock on Nuki’s 10th anniversary

NO COMMENTS

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Exit mobile version