Temporal.Duration.prototype.years
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 years của các instance Temporal.Duration trả về một số nguyên biểu diễn số năm trong duration.
Bạn có thể biết dấu của years bằng cách kiểm tra thuộc tính sign của duration.
Set accessor của years là undefined. 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 years
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.years); // 1
console.log(d2.years); // -1
console.log(d3.years); // 1
console.log(d4.years); // 0
// Cân bằng d4
const d4Balanced = d4.round({
largestUnit: "years",
relativeTo: Temporal.PlainDate.from("2021-01-01"), // ISO 8601 calendar
});
console.log(d4Balanced.years); // 1
console.log(d4Balanced.months); // 0
Đặc tả
| Thông số kỹ thuật |
|---|
| Temporal> # sec-get-temporal.duration.prototype.years> |
Tương thích trình duyệt
Xem thêm
Temporal.DurationTemporal.Duration.prototype.monthsTemporal.Duration.prototype.weeksTemporal.Duration.prototype.daysTemporal.Duration.prototype.hoursTemporal.Duration.prototype.minutesTemporal.Duration.prototype.secondsTemporal.Duration.prototype.millisecondsTemporal.Duration.prototype.microsecondsTemporal.Duration.prototype.nanoseconds