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