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

Temporal.PlainTime.from()

· 52CE 我爱测

The **`Temporal.PlainTime.from()`** static method creates a new `Temporal.PlainTime` object from another `Temporal.PlainTime` object, an object with time properties, or an RFC 9557 string.

The Temporal.PlainTime.from() static method creates a new Temporal.PlainTime object from another Temporal.PlainTime object, an object with time properties, or an RFC 9557 string.

Syntax

Temporal.PlainTime.from(info)
Temporal.PlainTime.from(info, options)

Parameters

  • info

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

      • A Temporal.PlainDateTime instance, which provides the time in the same fashion as Temporal/PlainDateTime/toPlainDate.

      • A Temporal.ZonedDateTime instance, which provides the time in the same fashion as Temporal/ZonedDateTime/toPlainDate.

      • An RFC 9557 string containing a time.

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

        • Temporal/PlainTime/hour
        • Temporal/PlainTime/microsecond
        • Temporal/PlainTime/millisecond
        • Temporal/PlainTime/minute
        • Temporal/PlainTime/nanosecond
        • Temporal/PlainTime/second

        They are truncated to be integers. Out-of-range values are handled by the overflow option.

  • options

    • : An object containing the following property:
      • overflow
        • : A string specifying the behavior when a time component is out of range (when using the object info). Possible values are:
          • "constrain" (default)
            • : The time component is clamped to the valid range.
          • "reject"
            • : A RangeError is thrown if the time component is out of range.

Return value

A new Temporal.PlainTime object, representing the time specified by info.

Exceptions

  • TypeError
    • : Thrown in one of the following cases:
      • info is not an object with at least one recognized property or a string.
      • options is not an object or undefined.
  • RangeError
    • : Thrown if the provided numerical properties are out of range, and options.overflow is set to "reject".

Examples

Creating a PlainTime from an object

const t1 = Temporal.PlainTime.from({ hour: 0 });
console.log(t1.toString()); // "00:00:00"

const t2 = Temporal.PlainTime.from({ hour: 12, minute: 34, second: 56 });
console.log(t2.toString()); // "12:34:56"

const t3 = Temporal.PlainTime.from({
  hour: 12,
  minute: 34,
  second: 56,
  millisecond: 123,
  microsecond: 456,
  nanosecond: 789,
});
console.log(t3.toString()); // "12:34:56.123456789"

Controlling overflow behavior

By default, out-of-range values are clamped to the valid range:

const t1 = Temporal.PlainTime.from({ hour: 25 });
console.log(t1.toString()); // "23:00:00"

const t2 = Temporal.PlainTime.from({ hour: 25, minute: 60 });
console.log(t2.toString()); // "23:59:00"

You can change this behavior to throw an error instead:

Temporal.PlainTime.from({ hour: 25 }, { overflow: "reject" });
// RangeError: time value "hour" not in 0..23: 25

Creating a PlainTime from a string

const t1 = Temporal.PlainTime.from("12:34:56.123456789");
console.log(t1.toLocaleString("en-US", { timeStyle: "full" }));
// 12:34:56 PM

Creating a PlainTime from another Temporal instance

const dt = Temporal.PlainDateTime.from("2021-07-01T12:00");
const t = Temporal.PlainTime.from(dt);
console.log(t.toString()); // "12:00:00"

const zdt = Temporal.ZonedDateTime.from(
  "2021-07-01T00:00+08:00[Asia/Shanghai]",
);
const t2 = Temporal.PlainTime.from(zdt);
console.log(t2.toString()); // "00:00:00"

const t3 = Temporal.PlainTime.from(t);
console.log(t3.toString()); // "12:00:00"

Specifications

Browser compatibility

See also

  • Temporal.PlainTime
  • Temporal/PlainTime/PlainTime
  • Temporal/PlainTime/with

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

资料来源与适用范围

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

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

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