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

Temporal.PlainTime.prototype.with()

· 52CE 我爱测

The **`with()`** method of Temporal.PlainTime instances returns a new `Temporal.PlainTime` object representing this time with some fields replaced by new values. Because all `Temporal` objects are designed to be immutable, this method essentially functions as the setter for the t

The with() method of Temporal.PlainTime instances returns a new Temporal.PlainTime object representing this time with some fields replaced by new values. Because all Temporal objects are designed to be immutable, this method essentially functions as the setter for the time's fields.

Syntax

with(info)
with(info, options)

Parameters

  • info
    • : An object containing at least one of the properties recognized by Temporal/PlainTime/from: hour, microsecond, millisecond, minute, nanosecond, second. Unspecified properties use the values from the original time.
  • options
    • : An object containing the following property:
      • overflow
        • : A string specifying the behavior when a time component is out of range. 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, where the fields specified in info that are not undefined are replaced by the corresponding values, and the rest of the fields are copied from the original time.

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

Using with()

const time = Temporal.PlainTime.from("12:34:56.123456789");
const newTime = time.with({ hour: 23 });
console.log(newTime.toString()); // '23:34:56.123456789'

For more examples, see the documentation for the individual properties that can be set using with().

Specifications

Browser compatibility

See also

  • Temporal.PlainTime
  • Temporal/PlainTime/from
  • Temporal/PlainTime/add
  • Temporal/PlainTime/subtract

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

资料来源与适用范围

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

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

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