Temporal.PlainDate.prototype.weekOfYear
Temporal.PlainDate
实例的 weekOfYear
访问器属性会返回一个正整数,表示此日期在其 yearOfWeek
中的 1 基数周索引,如果日历没有明确定义的周系统,则返回 undefined
。一年中的第一周是 1
。这取决于 日历。
请注意,对于 ISO 8601,一年中的最初几天和最后几天可能归属于上一年的最后一周或下一年的第一周。也就是说,如果一周跨越两年,则它属于拥有多数天数的年份。要获取 weekOfYear
所属的年份,请使用 yearOfWeek
属性,而不是 year
属性。
weekOfYear
的 set 访问器是 undefined
。您不能直接更改此属性。要创建一个具有所需新 weekOfYear
值的 Temporal.PlainDate
对象,请使用 add()
或 subtract()
方法,并提供相应的 weeks
数量。
示例
使用 weekOfYear
js
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
更改 weekOfYear
PlainDate
不支持直接更改 weekOfYear
。要更改周数,您需要先计算出与您期望的周数之间的差异,然后使用 add
或 subtract
来相应地调整日期。例如,要更改到上一周
js
const date = Temporal.PlainDate.from("2021-07-01");
const previousWeek = date.subtract({ weeks: 1 });
console.log(previousWeek.toString()); // 2021-06-24
规范
规范 |
---|
Temporal # sec-get-temporal.plaindate.prototype.weekofyear |
浏览器兼容性
加载中…