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

Temporal.Duration.from()

· 52CE 我爱测

The **`Temporal.Duration.from()`** static method creates a new `Temporal.Duration` object from another `Temporal.Duration` object, an object with duration properties, or an ISO 8601 string.

The Temporal.Duration.from() static method creates a new Temporal.Duration object from another Temporal.Duration object, an object with duration properties, or an ISO 8601 string.

Syntax

Temporal.Duration.from(info)

Parameters

  • info
    • : One of the following:
      • A Temporal.Duration instance, which creates a copy of the instance.

      • An ISO 8601 string representing a duration.

      • An object containing at least one of the following properties (in the order they are retrieved and validated):

        • Temporal/Duration/days
        • Temporal/Duration/hours
        • Temporal/Duration/microseconds
        • Temporal/Duration/milliseconds
        • Temporal/Duration/minutes
        • Temporal/Duration/months
        • Temporal/Duration/nanoseconds
        • Temporal/Duration/seconds
        • Temporal/Duration/weeks
        • Temporal/Duration/years

        Each property should contain an integer number value. The resulting duration must not have mixed signs, so all of these properties must have the same sign (or zero). Missing properties are treated as zero.

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 recognized properties in the info object 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>.
  • TypeError
    • : Thrown in one of the following cases:
      • info is not an object or a string.
      • All of the recognized properties in the info object are undefined.

Examples

Creating a duration from an object

const d1 = Temporal.Duration.from({ hours: 1, minutes: 30 });
console.log(d1.toString()); // "PT1H30M"

const d2 = Temporal.Duration.from({ months: 1, days: 2 });
console.log(d2.toString()); // "P1M2D"

// Uncommon because unbalanced, but valid
const unbalanced = Temporal.Duration.from({
  hours: 100,
  minutes: 100,
  seconds: 100,
});
console.log(unbalanced.toString()); // "PT100H100M100S"

const neg = Temporal.Duration.from({ hours: -1, minutes: -30 });
console.log(neg.toString()); // "-PT1H30M"

Creating a duration from a string

const d = Temporal.Duration.from("P1Y2M3W4DT5H6M7.00800901S");
console.log(d.hours); // 5

Creating a duration from another duration

const d1 = Temporal.Duration.from({ hours: 1, minutes: 30 });
const d2 = Temporal.Duration.from(d1);
console.log(d2.toString()); // "PT1H30M"

Specifications

Browser compatibility

See also

  • Temporal.Duration
  • Temporal/Duration/Duration
  • Temporal/Duration/with

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

资料来源与适用范围

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

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

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