This feature is not Baseline because it does not work in some of the most widely-used browsers.
The Temporal.PlainTime.compare() static method returns a number (-1, 0, or 1) indicating whether the first time comes before, is the same as, or comes after the second time. It is equivalent to comparing the hour, minute, second, millisecond, microsecond, and nanosecond fields one by one.
Temporal.PlainTime.compare(time1, time2)
time1A string, an object, or a Temporal.PlainTime instance representing the first time to compare. It is converted to a Temporal.PlainTime object using the same algorithm as Temporal.PlainTime.from().
time2The second time to compare, converted to a Temporal.PlainTime object using the same algorithm as time1.
Returns -1 if time1 comes before time2, 0 if they are the same, and 1 if time1 comes after time2.
const time1 = Temporal.PlainTime.from("12:34:56");
const time2 = Temporal.PlainTime.from("12:34:57");
console.log(Temporal.PlainTime.compare(time1, time2)); // -1
const time3 = Temporal.PlainTime.from("11:34:56");
console.log(Temporal.PlainTime.compare(time1, time3)); // 1
The purpose of this compare() function is to act as a comparator to be passed to Array.prototype.sort() and related functions.
const times = ["12:34:56", "11:34:56", "12:34:57"]; times.sort(Temporal.PlainTime.compare); console.log(times); // [ "11:34:56", "12:34:56", "12:34:57" ]
| Specification |
|---|
| Temporal> # sec-temporal.plaintime.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 | preview | 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/PlainTime/compare