0

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.?

2
  • No, that would be a very unusual API. Commented Jun 9, 2022 at 20:04
  • I agree! We've actually had quite a few requests for this during the design process of the Temporal API. So I wanted to post a Q&A pair here to help future developers having the same question to explain why a universal parsing function is not present. Commented Aug 19, 2022 at 23:30

1 Answer 1

1

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.

If you have a list of date/time strings and you don't know what type they are, but you'd like to parse them anyways, then it's fairly straightforward to write a function that calls the from methods of multiple Temporal types and returns the first result that doesn't throw. Just make sure to only include types that contain all the data you require, and make sure to put the types in order from "most data" to "least data" in the string. This will usually be this order:

  1. Temporal.ZonedDateTime

  2. Temporal.Instant

  3. Temporal.PlainDateTime

  4. Temporal.PlainDate

  5. Temporal.PlainTime

  6. Temporal.PlainYearMonth

  7. Temporal.PlainMonthDay

  8. Temporal.Duration (although honestly I doubt that there are many use cases where both duration data and date/time data is combined in the same input!)

If you're curious about why Temporal doesn't have a Temporal.parse method, more background can be found here: https://tc39.es/proposal-temporal/docs/parse-draft.html (I'm one of the champions of the Temporal proposal since 2020.) Here's one relevant excerpt:

This is a draft design document for a Temporal.parse API, which is not currently planned to be implemented for several reasons:

  • Temporal's approach to most operations—including parsing—is to encourage strong typing, e.g. Temporal.Instant.from vs. Temporal.PlainDateTime.from. A type-spanning "parse anything" API goes against that strongly-typed model.
  • The main use case beyond type-specific parsing that was identified for a parse API was handling "partially correct" ISO strings, e.g. where only one unit was out of range. Most of these use cases were addressed via the overflow option in the from method of all types which either clamps out-of-range values ('constrain') to the nearest in-range value or throws ('reject') in that case.
  • The final remaining case for a parse API was resolving the case where a time zone and a time zone offset can be in conflict, as would happen for future Temporal.ZonedDateTime values stored before a country permanently abolishes DST. This use case is now handled via the offset option of Temporal.ZonedDateTime.from.
Sign up to request clarification or add additional context in comments.

3 Comments

I would expect this hypothetical Temporal.parse() function to return the most specific type that can be created from the string, so a PlainDate from 2020-04-25, PlainMonthDay from 04-25, and PlainYearMonth from 2020-04
If we did have a Temporal.parse with that behavior, it'd be much more easily discoverable via IDE autocomplete than the type-specific parsing methods like Temporal.PlainDate.from. Many novice developers would likely end up using that method and assuming the result would always be the type they expect. Then they'd lose the ability to easily detect unexpected data in the input. This would likely lead to fragile code, and could even undermine one of the main benefits of Temporal which is for users to use a specific Temporal type for a specific kind of input data.
BTW, there's more discussion of a possible Temporal.parse method at tc39.es/proposal-temporal/docs/parse-draft.html, including some explanations of alternatives considered and why we decided not to include it in the proposal. (I've been one of the Temporal proposal's champions on TC39 since 2020.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.