Temporal.PlainDate.prototype.withCalendar()

可用性有限

此特性不是基線特性,因為它在一些最廣泛使用的瀏覽器中不起作用。

實驗性: 這是一項實驗性技術
在生產中使用此技術之前,請仔細檢查瀏覽器相容性表格

withCalendar() 方法是 Temporal.PlainDate 例項的一個方法,它返回一個新的 Temporal.PlainDate 物件,該物件表示當前日期在新的日曆系統中解析後的結果。由於所有 Temporal 物件都被設計為不可變的,因此此方法實際上充當了該日期的 calendarId 屬性的設定器。

要替換日期的元件屬性,請改用 with() 方法。

語法

js
withCalendar(calendar)

引數

calendar

一個字串,對應於 calendarId 屬性。請參閱 Intl.supportedValuesOf() 獲取常用日曆型別的列表。

返回值

一個新的 Temporal.PlainDate 物件,表示原始 PlainDate 指定的日期,但在新的日曆系統中解析。

異常

TypeError

如果 calendar 不是字串,則丟擲錯誤。

RangeError

如果 calendar 不是有效的日曆識別符號,則丟擲錯誤。

示例

使用 withCalendar()

js
const date = Temporal.PlainDate.from("2021-07-01");
const newDate = date.withCalendar("islamic-umalqura");
console.log(newDate.toLocaleString("en-US", { calendar: "islamic-umalqura" }));
// 11/21/1442 AH

規範

規範
Temporal
# sec-temporal.plaindate.prototype.withcalendar

瀏覽器相容性

另見