Fixing bug-prone JavaScript dates: Deno 1.4 adds its support for Temporal API

Fixing bug-prone JavaScript dates: Deno 1.4 adds its support for Temporal API
Calendar

Deno 1.4 is out, including support (behind a flag) for the Temporal API, a JavaScript standard for improved date handling which is inching towards readiness after years in development.

JavaScript is the world’s most popular programming language by most measures, but has problematic date handling. The Temporal API is an ECMAScript proposal that “brings a modern date/time API to the ECMAScript language,” according to its documentation, ECMAScript being the official name of the JavaScript standard. The proposal is now at Stage 3 which means it is a candidate proposal, the last stage before approval.

Date handling is of critical importance in many applications, and although developers have learned to live with quirks in JavaScript date functions, an improved API is welcome. Some of the issues with JavaScript dates are described by Maggie Johnson-Pint, one of the authors of the Temporal API. For example, she notes that methods on DataScript date objects, such as setMonth, change its value, whereas a better approach is for these methods to return new Date values. The existing approach is bug-prone. 

Another issue is with parsing date strings which has a bug relating to whether time values are interpreted as local or as UTC (Universal Time); when the standards committee tried to fix the bug they could not as it broke too much existing code.

Temporal is a new namespace, ensuring that there is no confusion between existing and new Date objects. It supports objects like PlainDateTime which do not carry time zone information, and ZonedDateTime which do have a time zone. Methods like add, which adds a duration to the date/time value, return a new object rather than modifying the existing one. It is a developer-friendly API.

Although first proposed in 2017, the Temporal API remains in development in Chrome, and in JavaScript Core, used by Apple Safari. Mozilla has marked it “Resolved Fixed” in SpiderMonkey, the JavaScript engine used by Firefox. According to the post from the Deno team, it is “actively implemented by all major JavaScript engines,” with the added comment that it is unlikely the API will be changed. The aim is to stabilize it in the forthcoming Deno 2.

Another new features in Deno 1.4 is Decorators, an implementation of the ECMAScript Decorators proposal to extend JavaScript classes which is also Stage 3. However this will only work in TypeScript as the V8 engine used by Deno does not yet support it.

There are deprecations to note too, including the global window object and the old Deno.run() command which the team says is “old and error-prone.” Deno 2 is also going to remove resource ids, in favour of resources referenced by native JavaScript objects.

Additional Node.js APIs have been added, for easier porting of Node.js code, and there are bug-fixes to existing ones.

Finally, the team is removing the broad –unstable flag, which enables all unstable features, in favour of the granular flags like –unstable-temporal. The broad unstable flag will disappear in Deno 2.