Sub topic 22
Using Non-ISO Calendars
The earlier chapters used the ISO 8601 calendar for every value. That calendar is the proleptic Gregorian calendar, and Temporal names it iso8601. Many people plan their lives with other calendars. The Hebrew calendar sets Jewish holidays. The Islamic calendars set Ramadan. The Japanese calendar counts years by imperial era. Temporal supports these calendars directly, so you don't need a separate library.
This chapter assumes you already know the Plain types, from(), arithmetic with add and with, until, and toLocaleString from earlier chapters. The examples expect Temporal to be available, either natively or through the polyfill shown in the runtime support chapter.
How Temporal models a calendar
Every PlainDate, PlainDateTime, PlainYearMonth, PlainMonthDay, and ZonedDateTime carries a calendar ID, which is a string such as 'iso8601', 'hebrew', or 'japanese'. Internally, every value still stores an ISO date. The calendar only controls how that date is split into fields such as year, month, and day, and how arithmetic moves through months and years.
PlainTime and Instant have no calendar. A time of day or an exact moment means the same thing in every calendar.
Switching calendars
To view an existing date in another calendar, call withCalendar. The underlying day stays the same and only the fields change.
const iso = Temporal.PlainDate.from('2024-03-15');
const hebrew = iso.withCalendar('hebrew');
console.log(hebrew.calendarId); // 'hebrew'
console.log(hebrew.year); // 5784
console.log(hebrew.month); // 7
console.log(hebrew.monthCode); // 'M06'
console.log(hebrew.day); // 5
console.log(hebrew.monthsInYear); // 13
console.log(hebrew.inLeapYear); // true
console.log(hebrew.toString()); // '2024-03-15[u-ca=hebrew]'The string form keeps the ISO date and adds a [u-ca=hebrew] annotation. When you pass that string back to from(), you get the same value in the same calendar, so you can serialize and store these dates the way the serialization chapter describes.
You can create a date directly in a calendar by putting calendar in the property bag:
const japanese = Temporal.PlainDate.from({
calendar: 'japanese',
era: 'reiwa',
eraYear: 6,
month: 3,
day: 15,
});
console.log(japanese.toString()); // '2024-03-15[u-ca=japanese]'The current date has no calendar-specific shortcut. Temporal.Now only offers ISO methods, so call Temporal.Now.plainDateISO().withCalendar('hebrew').
To list the calendar IDs your runtime knows, call Intl.supportedValuesOf('calendar'). The list typically contains buddhist, chinese, coptic, dangi, ethiopic, gregory, hebrew, indian, islamic-civil, islamic-umalqura, japanese, persian, and roc.
month vs monthCode
The month field is the position of the month within its year. In calendars with leap months, that position changes from year to year. In a Hebrew leap year, Adar I is month 6 and Adar II is month 7. In a common year, Adar is month 6.
The monthCode field is stable across years. 'M06' always means Adar (Adar II in a leap year), and 'M05L' means the leap month Adar I. When you identify a month across years, use monthCode.
const leapYear = Temporal.PlainDate.from({ calendar: 'hebrew', year: 5784, monthCode: 'M06', day: 1 });
const commonYear = Temporal.PlainDate.from({ calendar: 'hebrew', year: 5785, monthCode: 'M06', day: 1 });
console.log(leapYear.month); // 7
console.log(commonYear.month); // 6
try {
Temporal.PlainDate.from(
{ calendar: 'hebrew', year: 5785, monthCode: 'M05L', day: 1 },
{ overflow: 'reject' }
);
} catch (e) {
console.log(e instanceof RangeError); // true: 5785 has no Adar I
}With the default overflow: 'constrain', the missing leap month is replaced by the nearest real month (M06 here). Use reject when you need to know that the month didn't exist.
Arithmetic follows the calendar
The add, subtract, until, and since methods count months and years in the value's own calendar. The same two days can be a different number of months apart in different calendars:
const startIso = Temporal.PlainDate.from('2024-02-10'); // 1 Adar I 5784
const endIso = Temporal.PlainDate.from('2024-04-09'); // 1 Nisan 5784
console.log(startIso.until(endIso, { largestUnit: 'month' }).toString()); // 'P1M30D'
const startHeb = startIso.withCalendar('hebrew');
const endHeb = endIso.withCalendar('hebrew');
console.log(startHeb.until(endHeb, { largestUnit: 'month' }).toString()); // 'P2M'Days and weeks mean the same in every calendar, so largestUnit: 'day' gives P59D in both. Both values in until or since must use the same calendar. If they don't, the call throws a RangeError.
Equality, comparison, and formatting
The calendar matters for some operations and is ignored by others:
| Operation | Does the calendar matter? |
|---|---|
| `equals` | Yes. The same ISO date in two calendars is not equal. |
| `Temporal.PlainDate.compare` | No. It compares only the internal ISO dates. |
| `until` / `since` | Yes. The calendars must match. |
| `toLocaleString` | Yes. A non-ISO calendar must match the formatter's calendar. |
const a = Temporal.PlainDate.from('2024-03-15');
const b = a.withCalendar('hebrew');
console.log(a.equals(b)); // false
console.log(Temporal.PlainDate.compare(a, b)); // 0
console.log(b.toLocaleString('en-US-u-ca-hebrew', { dateStyle: 'long' }));
// e.g. '5 Adar II 5784'
console.log(a.toLocaleString('en-US-u-ca-hebrew', { dateStyle: 'long' }));
// ISO dates adopt the locale's calendar: e.g. '5 Adar II 5784'
// b.toLocaleString('en-US') -> RangeError (hebrew vs gregory)An iso8601 value takes on whatever calendar the locale uses. A value in any other calendar must match the formatter's calendar exactly. If they differ, Temporal throws instead of silently showing a different calendar.
Common errors
- Assuming
month12 is the last month. ReadmonthsInYearinstead, because Hebrew leap years have 13 months. - Using
yearalone in era-based calendars. A Japaneseyearis an arithmetic year. UseeraanderaYearfor display-style input. - Mixing calendars in
until, which throws. Convert one side withwithCalendarfirst. - Expecting
islamic-umalquraorchineseresults to match an observation-based almanac exactly. These calendars use calculated tables, and older runtimes may disagree on dates far in the past or future.
Exercise
You store a yearly reminder for 1 Adar I. Write code that, given a Hebrew year number, returns the PlainDate for that reminder and falls back to 1 Adar (M06) in years without a leap month. Which field do you use, and why not month: 6?
Show answer
Use monthCode. For example: function reminder(year) { return Temporal.PlainDate.from({ calendar: 'hebrew', year, monthCode: 'M05L', day: 1 }); }. The default overflow 'constrain' maps the missing M05L to M06 in common years, and you can check inLeapYear if you want to make the fallback explicit. month: 6 is Adar I only in leap years. In common years it is plain Adar, so it can't tell the two cases apart.
Exercise
Why does Temporal.PlainDate.compare(a, b) return 0 while a.equals(b) returns false for a = Temporal.PlainDate.from('2024-03-15') and b = a.withCalendar('hebrew')?
Show answer
compare looks only at the internal ISO date, which is the same for both, so the two values sort as the same day. equals also requires the calendar IDs to match, and 'iso8601' differs from 'hebrew'.