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

Temporal.Instant.prototype.since()

· 52CE 我爱测

The **`since()`** method of Temporal.Instant instances returns a new Temporal.Duration object representing the duration from another instant (in a form convertible by Temporal/Instant/from) to this instant. The duration is positive if the other instant is before this instant, and

The since() method of Temporal.Instant instances returns a new Temporal.Duration object representing the duration from another instant (in a form convertible by Temporal/Instant/from) to this instant. The duration is positive if the other instant is before this instant, and negative if after.

This method does this - other. To do other - this, use the Temporal/Instant/until method.

Syntax

since(other)
since(other, options)

Parameters

  • other
    • : A string or a Temporal.Instant instance representing an instant to subtract from this instant. It is converted to a Temporal.Instant object using the same algorithm as Temporal/Instant/from.
  • options
    • : An object containing the options for Temporal/Duration/round, which includes largestUnit, roundingIncrement, roundingMode, and smallestUnit. largestUnit and smallestUnit only accept the units: "hours", "minutes", "seconds", "milliseconds", "microseconds", "nanoseconds", or their singular forms. For largestUnit, the default value "auto" means "seconds" or smallestUnit, whichever is greater. For smallestUnit, the default value is "nanoseconds".

Return value

A new Temporal.Duration object representing the duration since other to this instant. The duration is positive if other is before this instant, and negative if after.

Exceptions

  • RangeError
    • : Thrown if any of the options is invalid.

Examples

Using since()

const lastUpdated = Temporal.Instant.fromEpochMilliseconds(1735235418000);
const now = Temporal.Now.instant();
const duration = now.since(lastUpdated, { smallestUnit: "minute" });
console.log(`Last updated ${duration.toLocaleString("en-US")} ago`);

Balancing the resulting duration

Because an instant does not carry calendar information, the resulting duration avoids calendar durations, which are ambiguous without a calendar and time reference. Therefore, the result is unbalanced because hours may be greater than 24. To balance the duration, round the resulting duration again with the desired largestUnit, passing a relativeTo that carries the calendar information.

const lastUpdated = Temporal.Instant.fromEpochMilliseconds(1735235418000);
const now = Temporal.Now.instant();
const duration = now.since(lastUpdated, { smallestUnit: "minutes" });
const roundedDuration = duration.round({
  largestUnit: "years",
  // Use the ISO calendar; you can convert to another calendar using
  // withCalendar()
  relativeTo: now.toZonedDateTimeISO("UTC"),
});
console.log(`Last updated ${roundedDuration.toLocaleString("en-US")} ago`);

Specifications

Browser compatibility

See also

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

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

资料来源与适用范围

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

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

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