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 |
瀏覽器相容性
載入中…