大网数据广告活动
Web 标准与前端开发

Temporal.PlainDate.prototype.month

· 52CE 我爱测

The **`month`** accessor property of Temporal.PlainDate instances returns a positive integer representing the 1-based month index in the year of this date. The first month of this year is `1`, and the last month is the Temporal/PlainDate/monthsInYear. It is calendar-dependent.

The month accessor property of Temporal.PlainDate instances returns a positive integer representing the 1-based month index in the year of this date. The first month of this year is 1, and the last month is the Temporal/PlainDate/monthsInYear. It is calendar-dependent.

Note that unlike Date.prototype.getMonth(), the index is 1-based. If the calendar has leap months, then the month with the same Temporal/PlainDate/monthCode may have different month indexes for different years.

[!NOTE] Do not use this property to identify the actual month, including its name. Use Temporal/PlainDate/monthCode for that purpose. Use month only for identifying months within the context of a year, or to figure out their order.

The set accessor of month is undefined. You cannot change this property directly. Use the Temporal/PlainDate/with method to create a new Temporal.PlainDate object with the desired new value.

Examples

Using month

const date = Temporal.PlainDate.from("2021-07-01"); // ISO 8601 calendar
console.log(date.monthCode); // "M07"
console.log(date.month); // 7

const date2 = Temporal.PlainDate.from("2021-05-01[u-ca=chinese]");
console.log(date2.monthCode); // "M03"
console.log(date2.month); // 3; it is March 20 in the Chinese calendar

const date3 = Temporal.PlainDate.from("2023-05-01[u-ca=chinese]");
console.log(date3.monthCode); // "M03"
console.log(date3.month); // 4, although it is also March (M03)!

const date4 = Temporal.PlainDate.from("2023-04-01[u-ca=chinese]");
console.log(date4.monthCode); // "M02L"
console.log(date4.month); // 3, this month is a leap month, i.e. a duplicate February

Looping through all months in a year

const year = Temporal.PlainDate.from("2021-07-14"); // An arbitrary date in the year
for (
  let month = year.with({ month: 1 });
  month.year === year.year;
  month = month.add({ months: 1 })
) {
  console.log(month.month);
}

Alternatively, this is also a safe way (unlike the day example):

for (let month = 1; month <= year.monthsInYear; month++) {
  const monthDate = year.with({ month });
}

Changing month

const date = Temporal.PlainDate.from("2021-07-01");
const newDate = date.with({ month: 2 });
console.log(newDate.toString()); // 2021-02-01

You can also use Temporal/PlainDate/add or Temporal/PlainDate/subtract to move a certain number of months from the current date.

const date = Temporal.PlainDate.from("2021-07-01");
const newDate = date.add({ months: 3 });
console.log(newDate.toString()); // 2021-10-01

By default, with() constrains the day to the range of valid values. Both of the following will set the month to the last month of the year:

const date = Temporal.PlainDate.from("2021-07-01");
const lastMonth = date.with({ month: date.monthsInYear }); // 2021-12-01
const lastMonth2 = date.with({ month: Number.MAX_VALUE }); // 2021-12-01

Specifications

Browser compatibility

See also

  • Temporal.PlainDate
  • Temporal/PlainDate/with
  • Temporal/PlainDate/add
  • Temporal/PlainDate/subtract
  • Temporal/PlainDate/year
  • Temporal/PlainDate/day
  • Temporal/PlainDate/monthCode
  • Temporal/PlainDate/daysInMonth
  • Temporal/PlainDate/monthsInYear

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。