Temporal.ZonedDateTime.prototype.until()

Limited availability

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

Phương thức until() của các thực thể Temporal.ZonedDateTime trả về một đối tượng Temporal.Duration mới biểu thị khoảng thời gian từ ngày-giờ này đến một ngày-giờ khác (dưới dạng có thể chuyển đổi bởi Temporal.ZonedDateTime.from()). Khoảng thời gian là dương nếu ngày-giờ kia sau ngày-giờ này, và âm nếu trước.

Phương thức này thực hiện other - this. Để thực hiện this - other, hãy sử dụng phương thức since().

Cú pháp

js
until(other)
until(other, options)

Tham số

other

Một chuỗi, một đối tượng, hoặc một thực thể Temporal.ZonedDateTime biểu thị ngày-giờ cần trừ từ ngày-giờ này. Nó được chuyển đổi thành đối tượng Temporal.ZonedDateTime bằng cùng thuật toán như Temporal.ZonedDateTime.from(). Nó phải có cùng lịch với this.

options Optional

Các tùy chọn tương tự như since().

Giá trị trả về

Một đối tượng Temporal.Duration mới biểu thị khoảng thời gian từ ngày-giờ này đến other. Khoảng thời gian là dương nếu other sau ngày-giờ này, và âm nếu trước.

Ngoại lệ

RangeError

Được ném ra trong một trong các trường hợp sau:

  • other có lịch khác với this.
  • Bất kỳ tùy chọn nào không hợp lệ.
  • other có múi giờ khác với this, và largestUnit"days" hoặc lớn hơn.

Ví dụ

Sử dụng until()

js
const flight = Temporal.ZonedDateTime.from(
  "2024-12-21T13:31:00-05:00[America/New_York]",
);
const now = Temporal.Now.zonedDateTimeISO("America/New_York").round("second");
if (Temporal.ZonedDateTime.compare(flight, now) < 0) {
  console.error(
    "The flight is already in the past. The result may not make sense.",
  );
}
const duration = now.until(flight, { largestUnit: "days" });
console.log(`The flight is in ${duration.toLocaleString("en-US")}`);

Để biết thêm ví dụ, xem since().

Đặc tả

Specification
Temporal
# sec-temporal.zoneddatetime.prototype.until

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

Xem thêm