Temporal.PlainMonthDay.prototype.monthCode
monthCode
访问器属性是 Temporal.PlainMonthDay
实例的一个属性,它返回一个特定于日历的字符串,表示此日期的月份。它依赖于 日历。
通常它是 M
加上两位数的月份数字。对于闰月,它是前一个月份代码后跟 L
(即使它在概念上是后一个月份的派生;例如,在希伯来日历中,Adar I 的代码是 M05L
,但 Adar II 的代码是 M06
)。如果闰月是该年的第一个月,则代码为 M00L
。
因为 month
是年内的索引,而 PlainMonthDay
没有年份,所以 PlainMonthDay
没有 month
属性。因此,monthCode
用于表示与年份无关的月份。
monthCode
的设置访问器是 undefined
。您不能直接更改此属性。请使用 with()
方法创建一个具有所需新值的 Temporal.PlainMonthDay
新对象。
有关一般信息和更多示例,请参见 Temporal.PlainDate.prototype.monthCode
。
示例
使用 monthCode
js
const md = Temporal.PlainMonthDay.from("07-01"); // ISO 8601 calendar
console.log(md.monthCode); // "M07"
const md2 = Temporal.PlainMonthDay.from("2021-05-01[u-ca=chinese]");
console.log(md2.monthCode); // "M03"
const md3 = Temporal.PlainMonthDay.from("2023-04-01[u-ca=chinese]");
console.log(md3.monthCode); // "M02L"
更改 monthCode
js
const md = Temporal.PlainMonthDay.from("07-01");
const newMD = md.with({ monthCode: "M03" });
console.log(newMD.toString()); // 03-01
对于其他日历,只要存在一个有效的月份-日期年份,该月份-日期就被认为是有效的,其底层参考年份可能会发生变化。例如
js
const md = Temporal.PlainMonthDay.from({
monthCode: "M02",
day: 30,
calendar: "hebrew",
});
console.log(md.toString()); // 1971-11-18[u-ca=hebrew]
console.log(md.toLocaleString("en-US", { calendar: "hebrew" })); // 30 Heshvan
// 30 Heshvan only exists in 1971, but this year is not a leap year
const newMD = md.with({ monthCode: "M05L" });
console.log(newMD.toString()); // 1970-03-08[u-ca=hebrew]
console.log(newMD.toLocaleString("en-US", { calendar: "hebrew" })); // 30 Adar I
规范
规范 |
---|
Temporal # sec-get-temporal.plainmonthday.prototype.monthcode |
浏览器兼容性
加载中…