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 |
浏览器兼容性
加载中…