The main use case is sorting a list of birthdays or anniversaries, holidays that fall on the same date each year, etc. This is easy enough when using the ISO calendar, and human calendars based on it. For example, you can do it in several ways if your instances are guaranteed to have the ISO calendar:
// Rely on the default behaviour of converting to a string and sorting lexicographically:
birthdays.sort();
// Sort by monthCode, then day
birthdays.sort((a, b) => {
if (a.monthCode === b.monthCode) return a.day - b.day;
return a.monthCode < b.monthCode ? -1 : 1;
});
// Sort by projecting into Temporal.PlainDate in a leap year
birthdays.sort((a, b) => {
const year = 2000;
return Temporal.PlainDate.compare(a.toPlainDate({ year }), b.toPlainDate({ year }));
});
However, if sorting in a calendar with leap months, this is not easy at all and requires making choices about how intercalary months are ordered.
Concerns:
PlainMonthDay is cyclical, i.e. is December 31 really after January 1 or before? However, this applies to PlainTime as well, and Temporal has a comparison function for that. In the original Temporal proposal, this was justified because:
comparing PlainTime was a more common operation than comparing PlainMonthDay;
PlainTime comparison is unambiguous (see corner cases below);
PlainTime comparison, like all the other types, does not have to consult the calendar.
Cultural expectations for what happens in intercalary months might differ. For example, does Temporal.PlainMonthDay.from({ monthCode: 'M05L', day: 15, calendar: 'chinese' }) come after Temporal.PlainMonthDay.from({ monthCode: 'M04L', day: 15, calendar: 'chinese' }), or is it undefined because these two months never occur in the same year?
Should there be a
Temporal.PlainMonthDay.compare()
static method? Follow up from https://github.com/tc39/proposal-temporal/issues/523.Advantages:
The main use case is sorting a list of birthdays or anniversaries, holidays that fall on the same date each year, etc. This is easy enough when using the ISO calendar, and human calendars based on it. For example, you can do it in several ways if your instances are guaranteed to have the ISO calendar:
However, if sorting in a calendar with leap months, this is not easy at all and requires making choices about how intercalary months are ordered.
Concerns:
Prior art:
Constraints / corner cases:
Temporal.PlainMonthDay.from({ monthCode: 'M05L', day: 15, calendar: 'chinese' })
come afterTemporal.PlainMonthDay.from({ monthCode: 'M04L', day: 15, calendar: 'chinese' })
, or is it undefined because these two months never occur in the same year?