Temporal.PlainYearMonth.prototype.calendarId
calendarId 訪問器屬性是 Temporal.PlainYearMonth 例項,用於返回一個字串,表示用於解釋內部 ISO 8601 日期的日曆。
有關常用日曆型別的列表,請參閱Intl.supportedValuesOf()。
calendarId 的 set 訪問器是 undefined。您不能直接更改此屬性。沒有簡單的方法可以建立一個具有不同日曆但表示相同年月的 Temporal.PlainYearMonth 物件,因此您需要首先使用 toPlainDate() 將其轉換為 Temporal.PlainDate 物件,更改日曆,然後再轉換回來。
示例
使用 calendarId
js
const ym = Temporal.PlainYearMonth.from("2021-07");
console.log(ym.calendarId); // "iso8601"; default
const ym2 = Temporal.PlainYearMonth.from("2021-07-01[u-ca=chinese]");
console.log(ym2.calendarId); // "chinese"
更改 calendarId
js
const ym = Temporal.PlainYearMonth.from("2021-07");
const newYM = ym
.toPlainDate({ day: 1 })
.withCalendar("chinese")
.toPlainYearMonth();
console.log(newYM.year, newYM.monthCode); // 2021 "M05"
const newYM2 = ym
.toPlainDate({ day: 31 })
.withCalendar("chinese")
.toPlainYearMonth();
console.log(newYM2.year, newYM2.monthCode); // 2021 "M06"
規範
| 規範 |
|---|
| Temporal # sec-get-temporal.plainyearmonth.prototype.calendarid |
瀏覽器相容性
載入中…