Temporal.ZonedDateTime.prototype.withCalendar()
Temporal.ZonedDateTime 实例的 withCalendar() 方法返回一个新的 Temporal.ZonedDateTime 对象,表示此日期时间在新日历系统中的解释。因为所有 Temporal 对象都被设计为不可变的,所以此方法本质上作为日期时间的 calendarId 属性的设置器。
要替换日期时间组件属性,请使用 with() 方法。要替换其时区,请使用 withTimeZone() 方法。
语法
js
withCalendar(calendar)
参数
calendar-
与
calendarId属性对应的字符串。有关常用受支持日历类型的列表,请参见Intl.supportedValuesOf()。
返回值
一个新的 Temporal.ZonedDateTime 对象,表示由原始 ZonedDateTime 指定的日期时间,在新日历系统中解释。
异常
TypeError-
如果
calendar不是字符串,则抛出错误。 RangeError-
如果
calendar不是有效的日历标识符,则抛出错误。
示例
使用 withCalendar()
js
const zdt = Temporal.ZonedDateTime.from(
"2021-07-01T12:34:56[America/New_York]",
);
const newZDT = zdt.withCalendar("islamic-umalqura");
console.log(newZDT.toLocaleString("en-US", { calendar: "islamic-umalqura" }));
// 11/21/1442 AH, 12:34:56 PM EDT
规范
| 规范 |
|---|
| Temporal # sec-temporal.zoneddatetime.prototype.withcalendar |
浏览器兼容性
加载中…