Temporal.PlainYearMonth.compare()

Limited availability

This feature is not Baseline because it does not work in some of the most widely-used browsers.

The Temporal.PlainYearMonth.compare() static method returns a number (-1, 0, or 1) indicating whether the first year-month comes before, is the same as, or comes after the second year-month. It is equivalent to comparing their underlying ISO 8601 dates. Two year-months from different calendars may be considered equal if they start on the same ISO date.

Note: PlainYearMonth objects keep track of a reference ISO day, which is also used in the comparison. This day is automatically set when using the Temporal.PlainYearMonth.from() method, but can be set manually using the Temporal.PlainYearMonth() constructor, causing two equivalent year-months to be considered different if they have different reference days. For this reason, you should avoid using the constructor directly and prefer the from() method.

Syntax

js
Temporal.PlainYearMonth.compare(yearMonth1, yearMonth2)

Parameters

yearMonth1

A string, an object, or a Temporal.PlainYearMonth instance representing the first year-month to compare. It is converted to a Temporal.PlainYearMonth object using the same algorithm as Temporal.PlainYearMonth.from().

yearMonth2

The second year-month to compare, converted to a Temporal.PlainYearMonth object using the same algorithm as yearMonth1.

Return value

Returns -1 if yearMonth1 comes before yearMonth2, 0 if they are the same, and 1 if yearMonth1 comes after yearMonth2. They are compared by their underlying date values (usually the first day of the month), ignoring their calendars.

Examples

Using Temporal.PlainYearMonth.compare()

js
const ym1 = Temporal.PlainYearMonth.from("2021-08");
const ym2 = Temporal.PlainYearMonth.from("2021-09");
console.log(Temporal.PlainYearMonth.compare(ym1, ym2)); // -1

const ym3 = Temporal.PlainYearMonth.from("2021-07");
console.log(Temporal.PlainYearMonth.compare(ym1, ym3)); // 1

Comparing year-months in different calendars

js
const ym1 = Temporal.PlainYearMonth.from({ year: 2021, month: 8 });
const ym2 = Temporal.PlainYearMonth.from({
  year: 2021,
  month: 8,
  calendar: "islamic-umalqura",
});
const ym3 = Temporal.PlainYearMonth.from({
  year: 2021,
  month: 8,
  calendar: "hebrew",
});
console.log(ym1.toString()); // "2021-08"
console.log(ym2.toString()); // "2582-12-17[u-ca=islamic-umalqura]"
console.log(ym3.toString()); // "-001739-04-06[u-ca=hebrew]"
console.log(Temporal.PlainYearMonth.compare(ym1, ym2)); // -1
console.log(Temporal.PlainYearMonth.compare(ym1, ym3)); // 1

Sorting an array of year-months

The purpose of this compare() function is to act as a comparator to be passed to Array.prototype.sort() and related functions.

js
const months = [
  Temporal.PlainYearMonth.from({ year: 2021, month: 8 }),
  Temporal.PlainYearMonth.from({
    year: 2021,
    month: 8,
    calendar: "islamic-umalqura",
  }),
  Temporal.PlainYearMonth.from({ year: 2021, month: 8, calendar: "hebrew" }),
];

months.sort(Temporal.PlainYearMonth.compare);
console.log(months.map((d) => d.toString()));
// [ "-001739-04-06[u-ca=hebrew]", "2021-08", "2582-12-17[u-ca=islamic-umalqura]" ]

Specifications

Specification
Temporal
# sec-temporal.plainyearmonth.compare

Browser compatibility

desktop mobile server
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Bun
Deno
Node.js
compare
Chrome – Full support
Chrome 144 (Release date: 2026-01-13)
footnote Full support
Edge – Full support
Edge 144 (Release date: 2026-01-21)
footnote Full support
Firefox – Full support
Firefox 139 (Release date: 2025-05-27)
footnote Full support
Opera – Full support
Opera 128 (Release date: 2026-02-26)
footnote Full support
Safari – No support
Safari
footnote
footnote See bug 223166
Chrome Android – Full support
Chrome Android 144 (Release date: 2026-01-13)
footnote Full support
Firefox for Android – Full support
Firefox for Android 139 (Release date: 2025-05-27)
footnote Full support
Opera Android – Full support
Opera Android 95 (Release date: 2026-02-11)
footnote Full support
Safari on iOS – No support
Safari on iOS
footnote
footnote See bug 223166
Samsung Internet – No support
Samsung Internet
footnote No support
WebView Android – Full support
WebView Android 144 (Release date: 2026-01-13)
footnote Full support
WebView on iOS – No support
WebView on iOS
footnote
footnote See bug 223166
Bun – No support
Bun
footnote No support
Deno – Full support
Deno 2.7 (Release date: 2026-02-25)
footnote Full support
Node.js – No support
Node.js
footnote No support

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
No support
No support
See implementation notes.

See also