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

Temporal.Duration() constructor

· 52CE 我爱测

The **`Temporal.Duration()`** constructor creates Temporal.Duration objects.

The Temporal.Duration() constructor creates Temporal.Duration objects.

This constructor allows you to create instances by directly supplying the underlying data. Like all other Temporal classes, you should usually construct Temporal.Duration objects using the Temporal/Duration/from static method, which can handle a variety of input types.

Syntax

new Temporal.Duration()
new Temporal.Duration(years)
new Temporal.Duration(years, months)
new Temporal.Duration(years, months, weeks)
new Temporal.Duration(years, months, weeks, days)
new Temporal.Duration(years, months, weeks, days, hours)
new Temporal.Duration(years, months, weeks, days, hours, minutes)
new Temporal.Duration(years, months, weeks, days, hours, minutes, seconds)
new Temporal.Duration(years, months, weeks, days, hours, minutes, seconds, milliseconds)
new Temporal.Duration(years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds)
new Temporal.Duration(years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds)

[!NOTE] Temporal.Duration() can only be constructed with new. Attempting to call it without new throws a TypeError.

Parameters

  • years
    • : Number of years, or undefined (which is treated as 0).
  • months
    • : Number of months, or undefined (which is treated as 0).
  • weeks
    • : Number of weeks, or undefined (which is treated as 0).
  • days
    • : Number of days, or undefined (which is treated as 0).
  • hours
    • : Number of hours, or undefined (which is treated as 0).
  • minutes
    • : Number of minutes, or undefined (which is treated as 0).
  • seconds
    • : Number of seconds, or undefined (which is treated as 0).
  • milliseconds
    • : Number of milliseconds, or undefined (which is treated as 0).
  • microseconds
    • : Number of microseconds, or undefined (which is treated as 0).
  • nanoseconds
    • : Number of nanoseconds, or undefined (which is treated as 0).

Return value

A new Temporal.Duration object, possibly unbalanced, with the specified components.

Exceptions

  • RangeError
    • : Thrown in one of the following cases:
      • Any of the parameters is not an integer (including non-finite values).
      • A calendar unit (years, months, weeks) has an absolute value ≥ 2<sup>32</sup>.
      • The non-calendar part of the duration (days and below), when expressed in seconds, has an absolute value ≥ 2<sup>53</sup>.

Examples

Using Temporal.Duration()

const d = new Temporal.Duration(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
console.log(d.toString()); // "P1Y2M3W4DT5H6M7.00800901S"

Specifications

Browser compatibility

See also

  • Temporal.Duration
  • Temporal/Duration/from

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

资料来源与适用范围

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

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

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