This feature is not Baseline because it does not work in some of the most widely-used browsers.
The Temporal.Instant.compare() static method returns a number (-1, 0, or 1) indicating whether the first instant comes before, is the same as, or comes after the second instant. It is equivalent to comparing the epochNanoseconds of the two instants.
Temporal.Instant.compare(instant1, instant2)
instant1A string or a Temporal.Instant instance representing the first instant to compare. It is converted to a Temporal.Instant object using the same algorithm as Temporal.Instant.from().
instant2The second instant to compare, converted to a Temporal.Instant object using the same algorithm as instant1.
Returns -1 if instant1 comes before instant2, 0 if they are the same, and 1 if instant1 comes after instant2.
const instant1 = Temporal.Instant.from("2021-08-01T12:34:56Z");
const instant2 = Temporal.Instant.from("2021-08-01T12:34:56Z");
console.log(Temporal.Instant.compare(instant1, instant2)); // 0
const instant3 = Temporal.Instant.from("2021-08-01T13:34:56Z");
console.log(Temporal.Instant.compare(instant1, instant3)); // -1
The purpose of this compare() function is to act as a comparator to be passed to Array.prototype.sort() and related functions.
const instants = [
Temporal.Instant.from("2021-08-01T12:34:56Z"),
Temporal.Instant.from("2021-08-01T12:34:56+01:00"),
Temporal.Instant.from("2021-08-01T12:34:56-01:00"),
];
instants.sort(Temporal.Instant.compare);
console.log(instants.map((instant) => instant.toString()));
// [ '2021-08-01T11:34:56Z', '2021-08-01T12:34:56Z', '2021-08-01T13:34:56Z' ]
| Specification |
|---|
| Temporal> # sec-temporal.instant.compare> |
| 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 |
144 | 144 | 139 | No | No | 144 | 139 | No | No | No | 144 | No | ? | 1.40 | No |
© 2005–2025 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/Instant/compare