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 |
瀏覽器相容性
載入中…