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 |
瀏覽器相容性
載入中…