Temporal.Instant.prototype.epochNanoseconds

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 epochNanoseconds của các thực thể Temporal.Instant trả về một BigInt biểu diễn số nano giây đã trôi qua kể từ epoch Unix (nửa đêm đầu ngày 1 tháng 1 năm 1970, UTC) đến instant này.

Set accessor của epochNanosecondsundefined. Bạn không thể thay đổi thuộc tính này trực tiếp. Để tạo một đối tượng Temporal.Instant mới với giá trị epochNanoseconds mới mong muốn, hãy sử dụng phương thức tĩnh Temporal.Instant.fromEpochNanoseconds() thay thế.

Một instant chỉ có thể biểu diễn ±108 ngày (khoảng ±273.972,6 năm) quanh epoch, là ±8,64e21 nano giây. Cố gắng đặt epochNanoseconds vượt quá ranh giới này sẽ ném ra RangeError.

Ví dụ

Sử dụng epochNanoseconds

js
const instant = Temporal.Instant.from("2021-08-01T12:34:56.789Z");
console.log(instant.epochNanoseconds); // 1627821296789000000n

const instant2 = Temporal.Instant.from("1969-08-01T12:34:56.789Z");
console.log(instant2.epochNanoseconds); // -13173903211000000n

Thay đổi epochNanoseconds

Đây là phương thức cho phép bạn di chuyển bất kỳ lượng thời gian nào:

js
const instant = Temporal.Instant.from("2021-08-01T12:34:56.789Z");
const instant1hourLater = instant.add({ hours: 1 });
console.log(instant1hourLater.epochNanoseconds); // 1627824896789000000n

Nếu bạn đã biết sự thay đổi theo nano giây, bạn cũng có thể trực tiếp xây dựng một đối tượng Temporal.Instant mới:

js
const instant = Temporal.Instant.from("2021-08-01T12:34:56.789Z");
const instant1hourLater = Temporal.Instant.fromEpochNanoseconds(
  instant.epochNanoseconds + 3600000000000n,
);
console.log(instant1hourLater.epochNanoseconds); // 1627824896789000000n

Đặc tả

Specification
Temporal
# sec-get-temporal.instant.prototype.epochnanoseconds

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

Xem thêm