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.
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.
- No support for time zones
- Parsing is inconsistent and unreliable.
- Date object is mutable
- DST (Daylight Saving Time) behavior is unpredictable
- Computation APIs are difficult to work with
- 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.PlainDate
,Temporal.PlainTime
, andTemporal.PlainDateTime
) for objects which do not have an associated time zone information. - "Exact" (like
Temporal.Instant
andTemporal.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.