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

瀏覽器相容性

另見