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 |
浏览器兼容性
加载中…