Temporal.Instant.compare() - JavaScript | MDN

Syntax

js

Temporal.Instant.compare(instant1, instant2)

Parameters

instant1

A 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().

instant2

The second instant to compare, converted to a Temporal.Instant object using the same algorithm as instant1.

Return value

Returns -1 if instant1 comes before instant2, 0 if they are the same, and 1 if instant1 comes after instant2.

Examples

Using Temporal.Instant.compare()

js

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

Sorting an array of instants

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

js

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' ]

Specifications

Specification
Temporal
# sec-temporal.instant.compare

Browser compatibility

See also

Help improve MDN

Learn how to contribute

This page was last modified on by MDN contributors.