-
Notifications
You must be signed in to change notification settings - Fork 1
Open
Description
The toLocaleString() method of PlainYearMonth and PlainMonthDay usually requires some surprising extra manipulation in order for it to work. See tc39/proposal-temporal#262 (comment) for the rationale. A future change might make these toLocaleString() methods easier to use.
A sampler of the copious feedback we've gotten on this part of the proposal:
- Why can't I use toLocaleString on ISO PlainYearMonth? tc39/proposal-temporal#1221
- Always throwing RangeErrror in HandleDateTimeTemporalYearMonth and HandleDateTimeTemporalMonthDay tc39/proposal-temporal#2364
- The calendar inside Intl.DateTimeFormat and the calendar inside Temporal objects tc39/proposal-temporal#2521
- Printing in locale format using
toLocaleStringis verbose tc39/proposal-temporal#2542
Advantages:
Currently, to format a PlainYearMonth as human-readable, you have to do one of three things:
- Ensure the PlainYearMonth object is created with the locale's calendar
const localeCalendar = new Intl.DateTimeFormat().resolvedOptions().calendar;
const pym = Temporal.PlainYearMonth.from({ ...data, calendar: localeCalendar });
console.log(pym.toLocaleString());- Format into a locale whose calendar is the PlainYearMonth's calendar
console.log(pym.toLocaleString(undefined, { calendar: pym.calendarId });- Use a PlainDate instead
console.log(date.toLocaleString(undefined, { year: 'numeric', month: 'long' });These workarounds are surprising because they aren't necessary for other Temporal types (PlainDate, etc.) and unpleasantly verbose for what they do.
Concerns:
The original reasons for this decision still apply:
- It's not possible to convert a PlainYearMonth or PlainMonthDay 1:1 from one calendar to another directly, for this reason they don't have a
withCalendar()method. - If the conversion were allowed in cases where the calendars align, that would produce "data-driven exceptions" where programs would work in development (notably when converting 1:1 from the ISO 8601 calendar to Gregorian) and fail in production in other locales.
Prior art:
None yet
Constraints / corner cases:
None yet
jyasskin
Metadata
Metadata
Assignees
Labels
No labels