Temporal.PlainDate.prototype.until()

可用性有限

此特性不是基線特性,因為它在一些最廣泛使用的瀏覽器中不起作用。

實驗性: 這是一項實驗性技術
在生產中使用此技術之前,請仔細檢查瀏覽器相容性表格

until() 方法是 Temporal.PlainDate 例項的一個方法,它返回一個新的 Temporal.Duration 物件,表示從當前日期到另一個日期(可透過 Temporal.PlainDate.from() 轉換為 Temporal.PlainDate 物件)的持續時間。如果另一個日期在當前日期之後,則持續時間為正;如果在當前日期之前,則為負。

此方法執行 other - this。要執行 this - other,請使用 since() 方法。

語法

js
until(other)
until(other, options)

引數

其他

一個字串、物件或 Temporal.PlainDate 例項,表示要從中減去當前日期的日期。它使用與 Temporal.PlainDate.from() 相同的演算法轉換為 Temporal.PlainDate 物件。它必須與 this 具有相同的日曆。

options 可選

since() 相同的選項。

返回值

一個新 Temporal.Duration 物件,表示從當前日期直到 other 的持續時間。如果 other 在當前日期之後,則持續時間為正;如果在此之前,則為負。

異常

RangeError

在以下情況之一中丟擲

  • other 的日曆與 this 不同。
  • 任何選項無效。

示例

使用 until()

js
const launch = Temporal.PlainDate.from("2035-01-01");
const now = Temporal.Now.plainDateISO();
const duration = now.until(launch);
console.log(`It will be ${duration.toLocaleString("en-US")} until the launch`);

更多示例,請參閱 since()

規範

規範
Temporal
# sec-temporal.plaindate.prototype.until

瀏覽器相容性

另見