Temporal.Duration.prototype.weeks

Khả dụng hạn chế

This feature is not Baseline because it does not work in some of the most widely-used browsers.

Thuộc tính accessor weeks của các instance Temporal.Duration trả về một số nguyên biểu diễn số tuần trong duration.

Trừ khi duration được cân bằng, bạn không thể giả định phạm vi của giá trị này, nhưng bạn có thể biết dấu của nó bằng cách kiểm tra thuộc tính sign của duration. Nếu nó được cân bằng đến đơn vị lớn hơn tuần, phạm vi giá trị tuyệt đối của weeks phụ thuộc vào lịch (số tuần trong một tháng hoặc năm).

Set accessor của weeksundefined. Bạn không thể thay đổi thuộc tính này trực tiếp. Sử dụng phương thức with() để tạo một đối tượng Temporal.Duration mới với giá trị mới mong muốn.

Ví dụ

Sử dụng weeks

js
const d1 = Temporal.Duration.from({ weeks: 1, days: 1 });
const d2 = Temporal.Duration.from({ weeks: -1, days: -1 });
const d3 = Temporal.Duration.from({ weeks: 1 });
const d4 = Temporal.Duration.from({ days: 7 });

console.log(d1.weeks); // 1
console.log(d2.weeks); // -1
console.log(d3.weeks); // 1
console.log(d4.weeks); // 0

// Cân bằng d4
const d4Balanced = d4.round({
  largestUnit: "weeks",
  relativeTo: Temporal.PlainDate.from("2021-01-01"), // ISO 8601 calendar
});
console.log(d4Balanced.weeks); // 1
console.log(d4Balanced.days); // 0

Đặc tả

Thông số kỹ thuật
Temporal
# sec-get-temporal.duration.prototype.weeks

Tương thích trình duyệt

Xem thêm