Temporal.PlainDateTime.prototype.withPlainTime()

可用性有限

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

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

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

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

語法

js
withPlainTime()
withPlainTime(plainTime)

引數

plainTime 可選

表示新時間的字串、物件或 Temporal.PlainTime 例項。它使用與 Temporal.PlainTime.from() 相同的演算法轉換為 Temporal.PlainTime 物件。如果未指定,時間部分將設定為 00:00:00

返回值

一個新的 Temporal.PlainDateTime 物件,其日期部分從原始日期時間複製,時間部分被新時間替換。

示例

使用 withPlainTime()

js
const dt = Temporal.PlainDateTime.from("2021-07-01T12:34:56");

// You can pass a string
const newDT = dt.withPlainTime("13:45:00");
console.log(newDT.toString()); // "2021-07-01T13:45:00"

// 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 newDT2 = dt.withPlainTime({ hour: 13 });
console.log(newDT2.toString()); // "2021-07-01T13:00:00"

// You can pass nothing to set the time to midnight
const newDT3 = dt.withPlainTime();
console.log(newDT3.toString()); // "2021-07-01T00:00:00"

規範

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

瀏覽器相容性

另見