Temporal.Duration.prototype.subtract()

可用性有限

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

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

subtract() 方法是 Temporal.Duration 例項的方法,它返回一個新的 Temporal.Duration 物件,表示當前 duration 與給定 duration 之間的差值。它等同於加上另一個 duration 的負值

語法

js
subtract(other)

引數

其他

一個字串、一個物件或一個 Temporal.Duration 例項,表示要新增到當前 duration 的 duration。它會使用與 Temporal.Duration.from() 相同的演算法轉換為 Temporal.Duration 物件。

返回值

一個表示當前 duration 與 other 之間差值的新 Temporal.Duration 物件。

異常

RangeError

在以下情況之一中丟擲

  • thisother 是一個日曆 duration(即 yearsmonthsweeks 的值非零),因為在沒有日曆和時間參照的情況下,日曆 duration 是模糊的。
  • thisother 的差值溢位了可表示 duration 的最大值或最小值,即 ±253 秒。

示例

使用 subtract()

js
const d1 = Temporal.Duration.from({ hours: 1, minutes: 30 });
const d2 = Temporal.Duration.from({ hours: -1, minutes: -20 });

const d3 = d1.subtract(d2);
console.log(d3.toString()); // "PT2H50M"

有關更多示例和注意事項,請參閱 add() 方法。

規範

規範
Temporal
# sec-temporal.duration.prototype.subtract

瀏覽器相容性

另見