Temporal.ZonedDateTime.prototype.until()
Temporal.ZonedDateTime 例項的 until() 方法返回一個新的 Temporal.Duration 物件,表示從當前日期時間到另一個日期時間(可以透過 Temporal.ZonedDateTime.from() 轉換的形式)的持續時間。如果另一個日期時間在當前日期時間之後,則持續時間為正;如果在之前,則為負。
此方法執行 other - this。要執行 this - other,請使用 since() 方法。
語法
js
until(other)
until(other, options)
引數
其他-
一個字串、一個物件或一個
Temporal.ZonedDateTime例項,表示要從中減去當前日期時間的日期時間。它使用與Temporal.ZonedDateTime.from()相同的演算法轉換為Temporal.ZonedDateTime物件。它必須與this具有相同的日曆。 options可選-
與
since()相同的選項。
返回值
一個新的 Temporal.Duration 物件,表示從當前日期時間“直到”other 的持續時間。如果 other 在當前日期時間之後,則持續時間為正;如果在之前,則為負。
異常
RangeError-
在以下情況之一中丟擲
other的日曆與this不同。- 任何選項無效。
other的時區與this不同,並且largestUnit是"days"或更大的單位。
示例
使用 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")}`);
有關更多示例,請參見 since()。
規範
| 規範 |
|---|
| Temporal # sec-temporal.zoneddatetime.prototype.until |
瀏覽器相容性
載入中…