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

Temporal.ZonedDateTime.prototype.withPlainTime()

· 52CE 我爱测

The **`withPlainTime()`** method of Temporal.ZonedDateTime instances returns a new `Temporal.ZonedDateTime` object representing this date-time with the time part entirely replaced by the new time (in a form convertible by Temporal/PlainTime/from)

The withPlainTime() method of Temporal.ZonedDateTime instances returns a new Temporal.ZonedDateTime object representing this date-time with the time part entirely replaced by the new time (in a form convertible by Temporal/PlainTime/from)

This method will replace all time properties, defaulting to 0 where properties are unspecified. If you only want to replace some of the time properties, use the Temporal/ZonedDateTime/with method instead.

Syntax

withPlainTime()
withPlainTime(plainTime)

Parameters

  • plainTime
    • : A string, an object, or a Temporal.PlainTime instance representing the new time. It is converted to a Temporal.PlainTime object using the same algorithm as Temporal/PlainTime/from. If not specified, the time part is set to the start of the day (which is usually 00:00:00 unless it doesn't exist due to offset transitions). Disambiguation always happens in the "compatible" mode; if you want to use a different mode, use the Temporal/ZonedDateTime/with method instead.

Return value

A new Temporal.ZonedDateTime object, with the date part and the time zone copied from the original date-time and the time part replaced by the new time.

Examples

Using withPlainTime()

const zdt = Temporal.ZonedDateTime.from(
  "2021-07-01T12:34:56[America/New_York]",
);

// You can pass a string
const newZDT = zdt.withPlainTime("13:45:00");
console.log(newZDT.toString()); // "2021-07-01T13:45:00-04:00[America/New_York]"

// You can only specify some time properties, and the rest default to 0;
// for the with() method, they would be copied from the original date-time
const newZDT2 = zdt.withPlainTime({ hour: 13 });
console.log(newZDT2.toString()); // "2021-07-01T13:00:00-04:00[America/New_York]"

// You can pass nothing to set the time to midnight
const newZDT3 = zdt.withPlainTime();
console.log(newZDT3.toString()); // "2021-07-01T00:00:00-04:00[America/New_York]"

// But, if midnight doesn't exist, it may be a different time
const zdt2 = Temporal.ZonedDateTime.from(
  "2015-10-18T12:00-02:00[America/Sao_Paulo]",
);
console.log(zdt2.withPlainTime().toString()); // "2015-10-18T01:00:00-02:00[America/Sao_Paulo]"

Specifications

Browser compatibility

See also

  • Temporal.ZonedDateTime
  • Temporal/ZonedDateTime/with
  • Temporal/ZonedDateTime/withCalendar
  • Temporal/ZonedDateTime/withTimeZone
  • Temporal/ZonedDateTime/toPlainTime
  • Temporal/ZonedDateTime/from

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

资料来源与适用范围

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

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

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