Temporal.PlainMonthDay.prototype.day

可用性有限

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

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

Temporal.PlainMonthDay 例項的 **day** 訪問器屬性返回一個正整數,表示該日期月份中的 1 基日期索引,這與日曆上看到的日期數字相同。它 **依賴於日曆**。

day 的設定訪問器是 undefined。您不能直接更改此屬性。請使用 with() 方法建立一個具有所需新值的 Temporal.PlainMonthDay 新物件。

有關一般資訊和更多示例,請參見 Temporal.PlainDate.prototype.day

示例

使用 day

js
const md = Temporal.PlainMonthDay.from("07-01"); // ISO 8601 calendar
console.log(md.day); // 1

const md2 = Temporal.PlainMonthDay.from("2021-07-01[u-ca=chinese]");
console.log(md2.day); // 22; it is May 22 in the Chinese calendar

更改日期

js
const md = Temporal.PlainMonthDay.from("07-01");
const newMD = md.with({ day: 15 });
console.log(newMD.toString()); // 07-15

預設情況下,with() 會將日期限制在有效值範圍內。因此,您可以使用 { day: 1 } 將日期設定為月份的第一天,即使第一天沒有數字 1。類似地,以下程式碼會將日期設定為月份的最後一天。

js
const md = Temporal.PlainMonthDay.from("07-01");
const lastMD = md.with({ day: Number.MAX_VALUE }); // 07-31

對於 PlainMonthDay 而言,二月始終被認為有 29 天。

js
const md = Temporal.PlainMonthDay.from("02-01");
const lastMD = md.with({ day: Number.MAX_VALUE }); // 02-29
console.log(lastMD.day); // 29

對於其他日曆,只要存在該月日有效的年份,該月日就被認為是有效的,並且底層參考年份可能會因此而改變。例如:

js
const md = Temporal.PlainMonthDay.from({
  monthCode: "M02",
  day: 29,
  calendar: "hebrew",
});
console.log(md.toString()); // 1972-11-06[u-ca=hebrew]
console.log(md.toLocaleString("en-US", { calendar: "hebrew" })); // 29 Heshvan
const lastMD = md.with({ day: Number.MAX_VALUE });
// 30 Heshvan does not exist in 1972, so the reference year changes to 1971
console.log(lastMD.toString()); // 1971-11-18[u-ca=hebrew]
console.log(lastMD.toLocaleString("en-US", { calendar: "hebrew" })); // 30 Heshvan

規範

規範
Temporal
# sec-get-temporal.plainmonthday.prototype.day

瀏覽器相容性

另見