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

Temporal.PlainDate.prototype.dayOfYear

· 52CE 我爱测

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

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

The set accessor of dayOfYear is undefined. You cannot change this property directly. To create a new Temporal.PlainDate object with the desired new dayOfYear value, use the Temporal/PlainDate/add or Temporal/PlainDate/subtract method with the appropriate number of days.

Examples

Using dayOfYear

const date = Temporal.PlainDate.from("2021-07-01");
console.log(date.dayOfYear); // 182

const date2 = Temporal.PlainDate.from("2021-07-01[u-ca=chinese]");
console.log(date2.dayOfYear); // 140

const date3 = Temporal.PlainDate.from("2020-07-01");
console.log(date3.dayOfYear); // 183; 2020 is a leap year

Changing dayOfYear

PlainDate does not support changing dayOfYear directly. To change the day of the year, you need to first figure out the difference in days to your desired day of the year, then use add or subtract to adjust the date accordingly. For example, to change to the 100th day of this year (whether before or after):

function getDayInSameYear(date, destDayOfYear) {
  return date.add({ days: destDayOfYear - date.dayOfYear });
}

console.log(
  getDayInSameYear(Temporal.PlainDate.from("2021-07-01"), 100).toString(),
); // 2021-04-10
console.log(
  getDayInSameYear(Temporal.PlainDate.from("2021-01-01"), 100).toString(),
); // 2021-04-10
console.log(
  getDayInSameYear(Temporal.PlainDate.from("2020-01-01"), 100).toString(),
); // 2020-04-09

By default, with() constrains the day to the range of valid values. So you can always use { month: 1, day: 1 } to set the day to the first day of the year, even if the first day does not have the number 1. Similarly, the following will set the day to the last day of the year, regardless of how many days are in the last month or year:

const date = Temporal.PlainDate.from("2021-07-01");
const lastDay = date.with({ month: Number.MAX_VALUE, day: Number.MAX_VALUE }); // 2021-12-31

Specifications

Browser compatibility

See also

  • Temporal.PlainDate
  • Temporal/PlainDate/with
  • Temporal/PlainDate/add
  • Temporal/PlainDate/subtract
  • Temporal/PlainDate/year
  • Temporal/PlainDate/day
  • Temporal/PlainDate/dayOfWeek
  • Temporal/PlainDate/daysInYear

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

资料来源与适用范围

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

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

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