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

Temporal.PlainDate.prototype.weekOfYear

· 52CE 我爱测

The **`weekOfYear`** accessor property of Temporal.PlainDate instances returns a positive integer representing the 1-based week index in the Temporal/PlainDate/yearOfWeek of this date, or `undefined` if the calendar does not have a well-defined week system. The first week of the

The weekOfYear accessor property of Temporal.PlainDate instances returns a positive integer representing the 1-based week index in the Temporal/PlainDate/yearOfWeek of this date, or undefined if the calendar does not have a well-defined week system. The first week of the year is 1. It is calendar-dependent.

Note that for ISO 8601, the first and last few days of the year may be attributed to the last week of the previous year or the first week of the next year. Namely, if a week crosses two years, then it belongs to the year that has the majority of its days. To get the year that the weekOfYear belongs to, use the Temporal/PlainDate/yearOfWeek property, not the Temporal/PlainDate/year property.

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

Examples

Using weekOfYear

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

// If 01-01 is a Friday/Saturday/Sunday, it belongs to the last week of the previous year
const date2 = Temporal.PlainDate.from("2021-01-01");
console.log(date2.dayOfWeek); // 5
console.log(date2.weekOfYear); // 53; 2020 has 53 weeks
console.log(date2.yearOfWeek); // 2020

// Otherwise, it belongs to the first week of the year
const date3 = Temporal.PlainDate.from("2020-01-01");
console.log(date3.dayOfWeek); // 3
console.log(date3.weekOfYear); // 1
console.log(date3.yearOfWeek); // 2020

// Similarly, if 12-31 is a Monday/Tuesday/Wednesday, it belongs to the first week of the next year
const date4 = Temporal.PlainDate.from("2019-12-31");
console.log(date4.dayOfWeek); // 2
console.log(date4.weekOfYear); // 1
console.log(date4.yearOfWeek); // 2020

Changing weekOfYear

PlainDate does not support changing weekOfYear directly. To change the week, you need to first figure out the difference in weeks to your desired week, then use add or subtract to adjust the date accordingly. For example, to change to the previous week:

const date = Temporal.PlainDate.from("2021-07-01");
const previousWeek = date.subtract({ weeks: 1 });
console.log(previousWeek.toString()); // 2021-06-24

Specifications

Browser compatibility

See also

  • Temporal.PlainDate
  • Temporal/PlainDate/with
  • Temporal/PlainDate/add
  • Temporal/PlainDate/subtract
  • Temporal/PlainDate/yearOfWeek
  • Temporal/PlainDate/dayOfWeek
  • Temporal/PlainDate/daysInWeek
  • Temporal/PlainDate/daysInYear

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

资料来源与适用范围

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

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

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