-
I was surprised to find that
while https://github.com/js-temporal/temporal-polyfill/blob/main/lib/intl.ts#L584
This seems inconsistent to me. Can someone point me to where this is mandated by the spec, or is that something this implementation could change? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
This is by design — the reason it's inconsistent is because you can convert a PlainDate from one calendar system to another, but not a PlainYearMonth or PlainMonthDay. At least, not without additional information (like, supplying the missing day-of-month for PlainYearMonth or year for PlainMonthDay.) It's true that "iso8601" to "gregory" is one of the few cases where you can just map the ISO reference date onto the Gregorian reference date and it will be correct. But if we allowed that for certain calendar pairs, we'd have the problem that toLocaleString() might work for a developer converting ISO to Gregorian, but throw for a user whose locale had a different calendar selected. The simplest way to get localized formatting for PlainYearMonth and PlainMonthDay is to pass the calendar in the toLocaleString options. For example: plainYearMonth.toLocaleString(locale, { calendar: plainYearMonth.calendar }) Another way that is more complicated, but might be what you want in your application, is to supply the missing information for the conversion from one calendar to the other, in a way that works for your application. For example, for a PlainYearMonth you might decide that you want the PlainYearMonth in the locale's calendar that day 1 of the original PlainYearMonth falls on: const plainDate = plainYearMonth.toPlainDate({ day: 1 });
const convertedPlainYearMonth = plainDate.withCalendar(localeCalendar).toPlainYearMonth();
convertedPlainYearMonth.toLocaleString(); More background reading in tc39/proposal-temporal#262, tc39/proposal-temporal#1221 (comment), and the documentation. This is mandated by the spec in the §15.3, the additions to DateTimeFormat — for example, note the difference between HandleDateTimeTemporalDate and HandleDateTimeTemporalYearMonth. |
Beta Was this translation helpful? Give feedback.
This is by design — the reason it's inconsistent is because you can convert a PlainDate from one calendar system to another, but not a PlainYearMonth or PlainMonthDay. At least, not without additional information (like, supplying the missing day-of-month for PlainYearMonth or year for PlainMonthDay.) It's true that "iso8601" to "gregory" is one of the few cases where you can just map the ISO reference date onto the Gregorian reference date and it will be correct. But if we allowed that for certain calendar pairs, we'd have the problem that toLocaleString() might work for a developer converting ISO to Gregorian, but throw for a user whose locale had a different calendar selected.
The simpl…