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