Temporal.Duration.prototype.hours
Limited availability
This feature is not Baseline because it does not work in some of the most widely-used browsers.
Thuộc tính accessor hours của các instance Temporal.Duration trả về một số nguyên đại diện cho số giờ 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 giờ, giá trị tuyệt đối của hours sẽ nằm trong khoảng từ 0 đến 23, bao gồm cả hai đầu.
Set accessor của hours 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 hours
js
const d1 = Temporal.Duration.from({ hours: 1, minutes: 30 });
const d2 = Temporal.Duration.from({ hours: -1, minutes: -30 });
const d3 = Temporal.Duration.from({ days: 1 });
const d4 = Temporal.Duration.from({ hours: 24 });
console.log(d1.hours); // 1
console.log(d2.hours); // -1
console.log(d3.hours); // 0
console.log(d4.hours); // 24
// Balance d4
const d4Balanced = d4.round({ largestUnit: "days" });
console.log(d4Balanced.hours); // 0
console.log(d4Balanced.days); // 1
Đặc tả
| Specification |
|---|
| Temporal> # sec-get-temporal.duration.prototype.hours> |
Tương thích trình duyệt
Xem thêm
Temporal.DurationTemporal.Duration.prototype.yearsTemporal.Duration.prototype.monthsTemporal.Duration.prototype.weeksTemporal.Duration.prototype.daysTemporal.Duration.prototype.minutesTemporal.Duration.prototype.secondsTemporal.Duration.prototype.millisecondsTemporal.Duration.prototype.microsecondsTemporal.Duration.prototype.nanoseconds