Temporal.PlainDateTime.prototype.withCalendar()

可用性有限

此特性不是基线特性,因为它在一些最广泛使用的浏览器中不起作用。

实验性: 这是一项实验性技术
在生产中使用此技术之前,请仔细检查浏览器兼容性表格

Temporal.PlainDateTime 实例的 withCalendar() 方法会返回一个新的 Temporal.PlainDateTime 对象,该对象表示当前日期时间在新日历系统中解释的结果。由于所有 Temporal 对象都设计为不可变的,因此此方法本质上充当日期时间的 calendarId 属性的设置器。

要替换日期时间组件属性,请改用 with() 方法。

语法

js
withCalendar(calendar)

参数

calendar

一个字符串,对应于 calendarId 属性。有关常用日历类型的列表,请参阅 Intl.supportedValuesOf()

返回值

一个新的 Temporal.PlainDateTime 对象,表示原始 PlainDateTime 指定的日期时间,在新日历系统中进行了解释。

异常

TypeError

如果 calendar 不是字符串,则抛出错误。

RangeError

如果 calendar 不是有效的日历标识符,则抛出错误。

示例

使用 withCalendar()

js
const dt = Temporal.PlainDateTime.from("2021-07-01T12:34:56");
const newDT = dt.withCalendar("islamic-umalqura");
console.log(newDT.toLocaleString("en-US", { calendar: "islamic-umalqura" }));
// 11/21/1442 AH, 12:34:56 PM

规范

规范
Temporal
# sec-temporal.plaindatetime.prototype.withcalendar

浏览器兼容性

另见