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
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.ZonedDateTimebiểu thị ngày-giờ cần trừ từ ngày-giờ này. Nó được chuyển đổi thành đối tượngTemporal.ZonedDateTimebằng cùng thuật toán nhưTemporal.ZonedDateTime.from(). Nó phải có cùng lịch vớithis. optionsOptional-
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:
othercó lịch khác vớithis.- Bất kỳ tùy chọn nào không hợp lệ.
othercó múi giờ khác vớithis, vàlargestUnitlà"days"hoặc lớn hơn.
Ví dụ
>Sử dụng until()
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> |