大网数据广告活动
Web 标准与前端开发

Temporal.Duration.prototype.subtract()

· 52CE 我爱测

The **`subtract()`** method of Temporal.Duration instances returns a new `Temporal.Duration` object with the difference between this duration and a given duration. It is equivalent to adding the negated value of the other duration.

The subtract() method of Temporal.Duration instances returns a new Temporal.Duration object with the difference between this duration and a given duration. It is equivalent to adding the negated value of the other duration.

Syntax

subtract(other)

Parameters

  • other
    • : A string, an object, or a Temporal.Duration instance representing a duration to add to this duration. It is converted to a Temporal.Duration object using the same algorithm as Temporal/Duration/from.

Return value

A new Temporal.Duration object representing the difference of this duration and other.

Exceptions

  • RangeError
    • : Thrown in one of the following cases:
      • Either this or other is a calendar duration (it has a non-zero years, months, or weeks), because calendar durations are ambiguous without a calendar and time reference.
      • The difference of this and other overflows the maximum or underflows the minimum representable duration, which is ±2<sup>53</sup> seconds.

Examples

Using subtract()

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"

For more examples and caveats, see the add() method.

Specifications

Browser compatibility

See also

  • Temporal.Duration
  • Temporal/Duration/add
  • Temporal/Duration/negated

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。