js-temporal / temporal-polyfill

Polyfill for Temporal (under construction)
ISC License
529 stars 28 forks source link

Replacement for formatToParts #278

Closed megla-tlanghorst closed 8 months ago

megla-tlanghorst commented 8 months ago

With the changes from 4.4, doesn't work anymore on ZonedDateTime. We use Intl.DateTimeFormat.formatToParts() to extract names of the weekdays, which now doesn't work anymore.

Is there any replacement (planned?) to replace this functionality.

12wrigja commented 8 months ago

There are instructions in the release notes explaining the alternatives to that mechanism, depending on what time zone you want the formatting to be done in:

https://github.com/js-temporal/temporal-polyfill/blob/2238550d8132e4feaddd80284a65612a12d7aa35/CHANGELOG.md?plain=1#L6-L17

The change was originally made to avoid the ambiguity of which timezone to use when formatting the output - the timezone in the Intl.DateTimeFormat ctor, the time zone in the ZonedDateTime, or something else?

Can you elaborate on how creating a DateTimeFormat, converting the ZonedDateTime to an Instant, and formatting that Instant using formatToParts doesn't work?

ptomato commented 8 months ago

Is there any replacement (planned?) to replace this functionality.

There is a replacement in the pipeline, https://github.com/tc39/proposal-intl-zoneddatetimeformat, although it is at Stage 1 meaning the form of the replacement is not yet known.

That said, if you just need to extract the weekday names, I think you can still do that from a ZonedDateTime:

> Temporal.Now.zonedDateTimeISO().toLocaleString('en', {weekday: 'long'})
'Tuesday'
megla-tlanghorst commented 8 months ago

Can you elaborate on how creating a DateTimeFormat, converting the ZonedDateTime to an Instant, and formatting that Instant using formatToParts doesn't work?

Instants being UTC may flip weekdays, so I don't think that works?

Edit: Ahh I think I understand now, with the time zone of the format that should be avoided, thanks, the solution was there all along... Definitely excited to have some solid docs and guides in place once it ships.

That said, if you just need to extract the weekday names, I think you can still do that from a ZonedDateTime:

> Temporal.Now.zonedDateTimeISO().toLocaleString('en', {weekday: 'long'})
'Tuesday'

That should work, thanks.

ptomato commented 8 months ago

(You could also convert the ZonedDateTime to PlainDateTime, that would definitely not flip the weekday and you wouldn't have to set the time zone separately)