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

Temporal.Instant.prototype.subtract()

· 52CE 我爱测

The **`subtract()`** method of Temporal.Instant instances returns a new `Temporal.Instant` object representing this instant moved backward by a given duration (in a form convertible by Temporal/Duration/from).

The subtract() method of Temporal.Instant instances returns a new Temporal.Instant object representing this instant moved backward by a given duration (in a form convertible by Temporal/Duration/from).

If you want to subtract two instants and get a duration, use Temporal/Instant/since or Temporal/Instant/until instead.

Syntax

subtract(duration)

Parameters

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

Return value

A new Temporal.Instant object representing subtracting duration from this instant. If duration is positive, then the returned instant is earlier than this instant; if duration is negative, then the returned instant is later than this instant.

Exceptions

  • RangeError
    • : Thrown in one of the following cases:
      • duration is a calendar duration (it has a non-zero years, months, or weeks), or has a non-zero days, because calendar durations are ambiguous without a calendar and time reference.
      • The result is not in the representable range, which is ±10<sup>8</sup> days, or about ±273,972.6 years, from the Unix epoch.

Description

Subtracting a duration is equivalent to adding its negation, so all the same considerations apply.

Examples

Subtracting a Temporal.Duration

const instant = Temporal.Instant.fromEpochMilliseconds(1000);
const duration = Temporal.Duration.from("PT1S"); // One-second duration
const newInstant = instant.subtract(duration);
console.log(newInstant.epochMilliseconds); // 0

For more examples, see Temporal/Instant/add.

Specifications

Browser compatibility

See also

  • Temporal.Instant
  • Temporal.Duration
  • Temporal/Instant/add
  • Temporal/Instant/since
  • Temporal/Instant/until

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

资料来源与适用范围

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

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

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