Temporal - The future of date and time handling in JavaScript

When was the last time you did not struggle with handling date and time in your project ? Yes, we are all in the same boat when it comes to working with date and time in javascript.

Temporal - The future of date and time handling in JavaScript

When was the last time you did not struggle with handling date and time in your project ? 😃 Yes, we are all in the same boat when it comes to working with date and time in javascript. There are reasons for that. The native Date API of JavaScript provides almost no to little help for working with Date objects. As a last resort JavaScript developers look for libraries like date-fns and moment.js etc.

Here are some common problems with current Date API.

  1. No support for time zones
  2. Parsing is inconsistent and unreliable.
  3. Date object is mutable
  4. DST (Daylight Saving Time) behavior is unpredictable
  5. Computation APIs are difficult to work with
  6. No support for non-Gregorian calendars

This is just a small list, there are so many other problems, that developers face on daily basis.

There is actually a good news for you. People at ECMA are working on a solution for this called Temporal

Temporal

Temporal will be a global object just like Math object in JavaScript. It will provide an easy to use API for date and time manipulations, first class support for all the time-zones, safe computations for DST (Daylight Saving Time) and support for non-Gregorian calendars.

Temporal in now stage 3 proposal at ECAMScript and is in phase of ECMAScript engine implementation. Being in stage 3, it's very less likely to change drastically from its current API, But still changes may occur as the result of feedback from implementation in JS engines.

But you don't have to wait to try it as polyfill is already available.

Temporal API follows convention of using types.

  • "Plain" (like Temporal.PlainDateTemporal.PlainTime, and Temporal.PlainDateTime) for objects which do not have an associated time zone information.
  • "Exact" (like Temporal.Instant and Temporal.ZonedDateTime) for objects which have the time zone information.

Converting between these types can be ambiguous because of time zones and daylight saving time, and the Temporal API lets developers configure how this ambiguity is resolved.

The complete API documentation is available for Temporal here

I am linking bellow some useful resources on the subjects.

GitHub - tc39/proposal-temporal: Provides standard objects and functions for working with dates and times.
Provides standard objects and functions for working with dates and times. - tc39/proposal-temporal
Temporal documentation
A Guide to the Temporal API in JavaScript
We all know that working with dates in JavaScript is ugly. The clunky Date API has pushed most of us to alternative libraries. Enter the Temporal API!
Liked it ?

Read more