Temporal.PlainDateTime.prototype.until()
until() 方法是 Temporal.PlainDateTime 例項的一個方法,它返回一個新的 Temporal.Duration 物件,表示從當前日期時間到另一個日期時間(可透過 Temporal.PlainDateTime.from() 轉換)的持續時間。如果另一個日期時間在當前日期時間之後,則持續時間為正;如果在此日期時間之前,則為負。
此方法執行 other - this。要執行 this - other,請使用 since() 方法。
語法
js
until(other)
until(other, options)
引數
其他-
一個字串、一個物件或一個
Temporal.PlainDateTime例項,表示要從中減去當前日期時間的日期時間。它使用與Temporal.PlainDateTime.from()相同的演算法轉換為Temporal.PlainDateTime物件。它必須與this具有相同的日曆。 options可選-
與
since()相同的選項。
返回值
一個新 Temporal.Duration 物件,表示從當前日期時間直到 other 的持續時間。如果 other 在當前日期時間之後,則持續時間為正;如果在此日期時間之前,則為負。
異常
RangeError-
在以下情況之一中丟擲
other的日曆與this不同。- 任何選項無效。
示例
使用 until()
js
let nextBilling = Temporal.PlainDateTime.from({
year: Temporal.Now.plainDateISO().year,
month: 4,
day: 1,
});
const now = Temporal.Now.plainDateTimeISO().round("second");
if (Temporal.PlainDateTime.compare(nextBilling, now) < 0) {
nextBilling = nextBilling.add({ years: 1 });
}
const duration = now.until(nextBilling);
console.log(`${duration.toLocaleString("en-US")} until next billing`);
有關更多示例,請參閱 since()。
規範
| 規範 |
|---|
| Temporal # sec-temporal.plaindatetime.prototype.until |
瀏覽器相容性
載入中…