Temporal.PlainYearMonth.prototype.since()
Limited availability
This feature is not Baseline because it does not work in some of the most widely-used browsers.
Phương thức since() của các thực thể Temporal.PlainYearMonth trả về một đối tượng Temporal.Duration mới đại diện cho khoảng thời gian từ một year-month khác (ở dạng có thể chuyển đổi bởi Temporal.PlainYearMonth.from()) đến year-month này. Khoảng thời gian là dương nếu tháng kia trước tháng này, và âm nếu sau.
Phương thức này thực hiện this - other. Để thực hiện other - this, hãy dùng phương thức until().
Cú pháp
since(other)
since(other, options)
Tham số
other-
Một chuỗi, một đối tượng, hoặc một thực thể
Temporal.PlainYearMonthđại diện cho một year-month cần trừ từ year-month này. Nó được chuyển đổi thành đối tượngTemporal.PlainYearMonthbằng thuật toán tương tự nhưTemporal.PlainYearMonth.from(). Nó phải có cùng lịch vớithis. optionsOptional-
Một đối tượng chứa các tùy chọn cho
Temporal.Duration.prototype.round(), bao gồmlargestUnit,roundingIncrement,roundingModevàsmallestUnit.largestUnitvàsmallestUnitchỉ chấp nhận các đơn vị:"years","months", hoặc dạng số ít của chúng. Đối vớilargestUnit, giá trị mặc định"auto"có nghĩa là"years". Đối vớismallestUnit, giá trị mặc định là"months". Ngày hiện tại được dùng làm tùy chọnrelativeTo.
Giá trị trả về
Một đối tượng Temporal.Duration mới đại diện cho khoảng thời gian kể từ other đến year-month này. Khoảng thời gian là dương nếu other trước year-month này, và âm nếu sau.
Ngoại lệ
RangeError-
Được ném ra trong một trong các trường hợp sau:
othercó lịch khác vớithis.- Bất kỳ tùy chọn nào không hợp lệ.
Ví dụ
>Sử dụng since()
const lastUpdated = Temporal.PlainYearMonth.from("2022-01");
const now = Temporal.Now.plainDateISO().toPlainYearMonth();
const duration = now.since(lastUpdated);
console.log(`Last updated ${duration.toLocaleString("en-US")} ago`);
// Expected output: "Last updated [number] years, [number] months ago"
const duration2 = now.since(lastUpdated, { largestUnit: "months" });
console.log(`Last updated ${duration2.toLocaleString("en-US")} ago`);
// Expected output: "Last updated [number] months ago"
const duration3 = now.since(lastUpdated, { smallestUnit: "years" });
console.log(`Last updated ${duration3.toLocaleString("en-US")} ago`);
// Expected output: "Last updated [number] years ago"
Làm tròn kết quả
Theo mặc định, phần thập phân của smallestUnit bị cắt bớt. Bạn có thể làm tròn lên bằng cách dùng các tùy chọn roundingIncrement và roundingMode.
const ym1 = Temporal.PlainYearMonth.from("2022-01");
const ym2 = Temporal.PlainYearMonth.from("2022-11");
const duration = ym2.since(ym1, {
smallestUnit: "years",
roundingMode: "ceil",
});
console.log(duration.toString()); // "P1Y"
Lấy kết quả theo ngày
Theo mặc định, kết quả khoảng thời gian không bao giờ chứa ngày, vì PlainYearMonth không cung cấp độ chính xác ở mức ngày. Bạn có thể lấy kết quả theo ngày bằng cách chuyển đổi sang Temporal.PlainDate trước với một ngày rõ ràng.
const ym1 = Temporal.PlainYearMonth.from("2022-01");
const ym2 = Temporal.PlainYearMonth.from("2022-11");
const duration = ym2.toPlainDate({ day: 1 }).since(ym1.toPlainDate({ day: 1 }));
console.log(duration.toString()); // "P304D"
Đặc tả
| Specification |
|---|
| Temporal> # sec-temporal.plainyearmonth.prototype.since> |