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

Temporal.Duration.prototype.total()

· 52CE 我爱测

The **`total()`** method of Temporal.Duration instances returns a number representing the total duration in the given unit.

The total() method of Temporal.Duration instances returns a number representing the total duration in the given unit.

Syntax

total(unit)
total(options)

Parameters

  • unit
    • : A string representing the unit option. This is a convenience overload, so total(unit) is equivalent to total({ unit }), where unit is a string.
  • options
    • : An object containing some or all of the following properties (in the order they are retrieved and validated):
      • relativeTo
        • : A zoned or plain date(time) that provides the time and calendar information to resolve calendar durations (see the link for the general interpretation of this option). Required if either this or other is a calendar duration, or unit is a calendar unit.
      • unit
        • : Any of the temporal units: "years", "months", "weeks", "days", "hours", "minutes", "seconds", "milliseconds", "microseconds", "nanoseconds", or their singular forms.

Return value

A floating-point number representing the total duration in the given unit. May be inexact due to floating point precision limits.

Exceptions

  • RangeError
    • : Thrown in one of the following cases:
      • unit is not provided or is not a valid unit.
      • Either this or other is a calendar duration, or unit is a calendar unit, and relativeTo is not provided.

Description

If a relativeTo is provided, the result is calculated by adding the duration to the starting point, finding the difference between the result and the starting point (in nanoseconds), and then converting the difference to the requested unit by dividing by the appropriate number of nanoseconds per unit. Providing a zoned date-time allows daylight saving time and other time zone changes to be taken into account too; otherwise, 24-hour days are assumed.

If relativeTo is not provided, the result is calculated by converting the duration to nanoseconds and dividing by the appropriate number of nanoseconds per unit.

Examples

Using total()

const d = Temporal.Duration.from({ hours: 1, minutes: 30 });

console.log(d.total("minutes")); // 90
console.log(d.total("hours")); // 1.5

Total of a calendar duration

const d = Temporal.Duration.from({ months: 1 });

console.log(
  d.total({ unit: "days", relativeTo: Temporal.PlainDate.from("2021-01-01") }),
); // 31

Specifications

Browser compatibility

See also

  • Temporal.Duration
  • Temporal/Duration/round

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

资料来源与适用范围

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

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

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