Temporal.PlainTime.prototype.until()

可用性有限

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

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

until() 方法屬於 Temporal.PlainTime 例項,它返回一個新的 Temporal.Duration 物件,表示從當前時間到另一個時間(可由 Temporal.PlainTime.from() 轉換)的時間差。如果另一個時間在當前時間之後,則時間差為正;如果之前,則為負。

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

語法

js
until(other)
until(other, options)

引數

其他

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

options 可選

since() 相同的選項。

返回值

一個新的 Temporal.Duration 物件,表示從當前時間 *到* other 的時間差。如果 other 在當前時間之後,則時間差為正;如果之前,則為負。

異常

RangeError

如果任何選項無效,則丟擲。

示例

使用 until()

js
const lunchTime = Temporal.PlainTime.from("12:30:00");
const now = Temporal.Now.plainTimeISO();
const duration = now.until(lunchTime);
console.log(`It will be ${duration.toLocaleString("en-US")} until lunch`);
// Example output: "It will be 3 hr, 42 min, 21 sec, 343 ms, 131 μs, 718 ns until lunch"

更多示例,請參閱 since()

規範

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

瀏覽器相容性

另見