Temporal.Duration.prototype.months

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 months của các instance Temporal.Duration trả về một số nguyên đại diện cho số tháng 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 thành đơn vị trên tháng, phạm vi giá trị tuyệt đối của months phụ thuộc vào lịch (số tháng trong một năm).

Set accessor của monthsundefined. 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 months

js
const d1 = Temporal.Duration.from({ years: 1, months: 1 });
const d2 = Temporal.Duration.from({ years: -1, months: -1 });
const d3 = Temporal.Duration.from({ years: 1 });
const d4 = Temporal.Duration.from({ months: 12 });

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

// Balance d4
const d4Balanced = d4.round({
  largestUnit: "years",
  relativeTo: Temporal.PlainDate.from("2021-01-01"), // ISO 8601 calendar
});
console.log(d4Balanced.months); // 0
console.log(d4Balanced.years); // 1

Đặc tả

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

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

Xem thêm