Temporal.ZonedDateTime.prototype.withPlainTime()

可用性有限

此特性不是基線特性,因為它在一些最廣泛使用的瀏覽器中不起作用。

實驗性: 這是一項實驗性技術
在生產中使用此技術之前,請仔細檢查瀏覽器相容性表格

Temporal.ZonedDateTime 例項的 withPlainTime() 方法返回一個新的 Temporal.ZonedDateTime 物件,表示此日期-時間,其中時間部分完全替換為新的時間(形式可由 Temporal.PlainTime.from() 轉換)。

此方法將替換所有時間屬性,未指定的屬性預設為 0。如果只想替換部分時間屬性,請改用 with() 方法。

語法

js
withPlainTime()
withPlainTime(plainTime)

引數

plainTime 可選

一個字串、一個物件或一個 Temporal.PlainTime 例項,表示新的時間。它使用與 Temporal.PlainTime.from() 相同的演算法轉換為 Temporal.PlainTime 物件。如果未指定,時間部分將設定為一天開始(通常是 00:00:00,除非由於偏移量轉換而不存在)。消歧總是以 "compatible" 模式發生;如果想使用不同的模式,請改用 with() 方法。

返回值

一個新的 Temporal.ZonedDateTime 物件,其中日期部分和時區從原始日期-時間複製,時間部分替換為新的時間。

示例

使用 withPlainTime()

js
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]"

規範

規範
Temporal
# sec-temporal.zoneddatetime.prototype.withplaintime

瀏覽器相容性

另見