Temporal.Instant.compare()

Khả dụng hạn chế

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

Phương thức tĩnh Temporal.Instant.compare() trả về một số (-1, 0, hoặc 1) cho biết instant đầu tiên đến trước, bằng, hoặc sau instant thứ hai. Nó tương đương với việc so sánh epochNanoseconds của hai instant.

Cú pháp

js
Temporal.Instant.compare(instant1, instant2)

Tham số

instant1

Một chuỗi hoặc thực thể Temporal.Instant biểu diễn instant đầu tiên cần so sánh. Nó được chuyển đổi thành đối tượng Temporal.Instant bằng cùng thuật toán như Temporal.Instant.from().

instant2

Instant thứ hai để so sánh, được chuyển đổi thành đối tượng Temporal.Instant bằng cùng thuật toán như instant1.

Giá trị trả về

Trả về -1 nếu instant1 đến trước instant2, 0 nếu chúng bằng nhau, và 1 nếu instant1 đến sau instant2.

Ví dụ

Sử dụng 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

Sắp xếp một mảng các instant

Mục đích của hàm compare() này là hoạt động như một bộ so sánh được truyền vào Array.prototype.sort() và các hàm liên quan.

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

Đặc tả

Thông số kỹ thuật
Temporal
# sec-temporal.instant.compare

Tương thích trình duyệt

Xem thêm