javascriptdatedatetimetimeecmascript-temporal

In JavaScript, what is the correct `Temporal` type to use for parsing a particular string?


ECMAScript's new Temporal proposal defines (finally!) a modern API for handling dates and times in JS, including timezone-safe arithmetic and non-Gregorian calendars.

I know I can use ISO 8601 or RFC 3339 strings with Temporal. If I have string data that I want to parse into a JS date/time object, which Temporal type should I use?


Solution

  • To determine the Temporal class that should be used to parse a string, it's important to pick the type whose data model matches the data in the string.

    Note that localized string formats like '12/25/2022' and '25/12/2022' are not parseable by Temporal. Only unambiguous, computer-readable formats can be parsed by Temporal, like '2022-12-25'.