Temporal.PlainDate.prototype.weekOfYear
Limited availability
This feature is not Baseline because it does not work in some of the most widely-used browsers.
Experimental: This is an experimental technology
Check the Browser compatibility table carefully before using this in production.
The weekOfYear
accessor property of Temporal.PlainDate
instances returns a positive integer representing the 1-based week index in the 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 yearOfWeek
property, not the 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 add()
or 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
Specification |
---|
Temporal proposal # sec-get-temporal.plaindate.prototype.weekofyear |
Browser compatibility
Report problems with this compatibility data on GitHubdesktop | mobile | server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
weekOfYear |
Legend
Tip: you can click/tap on a cell for more information.
- No support
- No support
- Experimental. Expect behavior to change in the future.
- See implementation notes.
- User must explicitly enable this feature.