Dates and Times in JavaScript – Igalia Compilers Team

blogs.igalia.com
3 min read
fairly easy
Article URL: https://blogs.igalia.com/compilers/2020/06/23/dates-and-times-in-javascript Comments URL: https://news.ycombinator.com/item?id=23639406 Points: 1 # Comments: 0
JavaScript Date is broken in ways that cannot be fixed without breaking the web. As the story goes, it was included in the original 10-day JavaScript engine hack and based on java.util.Date, which itself was deprecated in 1997 due to being a terrible API and replaced with a better one. The result has been for all of JavaScript's history, the built-in Date has remained very hard to work with directly.

Starting a few years ago, a proposal has been developing, to add a new globally available object to JavaScript, Temporal . Temporal is a robust and modern API for working with dates, times, and timestamps, and also makes it easy to do things that were hard or impossible with Date , like converting dates between time zones, adding and subtracting while accounting for daylight saving time, working with date-only or time-only data, and even handling dates in non-Gregorian calendars. Although Temporal has "just works" defaults, it also provides fine-grained opt-in control of overflows, interpreting ambiguous times, and other corner cases. For more on the history of the proposal, and why it's not possible to fix Date itself, read Maggie Pint's two-part blog post "Fixing JavaScript Date".

For examples of the power of Temporal, check out the cookbook. Many of these examples would be difficult to do with legacy Date , particularly the ones involving time zones. (We would have put an example in this post, but the code might soon become stale, for reasons which will hopefully become clear!)

This proposal is currently at Stage 2…
Fez Vrasta
Read full article