I've got a list of ISO 8601-compatible strings, some of which are dates (like '2022-12-25'
), some are date-time values (like '2022-12-25T12:00'
), and some have time zone info (like '2022-12-25T12:00-08:00[America/Los_Angeles]'
.
Using the new Temporal
API that's coming to JavaScript, is there one function I can call to parse all these strings into their corresponding Temporal
types like Temporal.PlainDate
, Temporal.PlainDateTime
, Temporal.ZonedDateTime
, etc.?
No, there is not a universal parse-into-any-Temporal
-type function that's built into the language.
This is because the same string can be used to parse many different Temporal
types.
To parse a string into a Temporal
type, you must know ahead of time what type you want.
For example, 2020-04-25[u-ca=hebrew]
can be successfully parsed by Temporal.PlainDate.from
, Temporal.PlainMonthDay.from
, Temporal.PlainYearMonth.from
, or even Temporal.PlainDateTime.from
.
This ambiguity requires choosing a Temporal
type before parsing.