Intl.RelativeTimeFormat

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since September 2020.

Đối tượng Intl.RelativeTimeFormat cho phép định dạng thời gian tương đối theo ngôn ngữ.

Try it

const rtf1 = new Intl.RelativeTimeFormat("en", { style: "short" });

console.log(rtf1.format(3, "quarter"));
// Expected output: "in 3 qtrs."

console.log(rtf1.format(-1, "day"));
// Expected output: "1 day ago"

const rtf2 = new Intl.RelativeTimeFormat("es", { numeric: "auto" });

console.log(rtf2.format(2, "day"));
// Expected output: "pasado mañana"

Hàm khởi tạo

Intl.RelativeTimeFormat()

Tạo một đối tượng Intl.RelativeTimeFormat mới.

Phương thức tĩnh

Intl.RelativeTimeFormat.supportedLocalesOf()

Trả về một mảng chứa các locale được cung cấp mà được hỗ trợ mà không phải dùng locale mặc định của runtime.

Thuộc tính instance

Các thuộc tính này được định nghĩa trên Intl.RelativeTimeFormat.prototype và dùng chung cho tất cả các instance Intl.RelativeTimeFormat.

Intl.RelativeTimeFormat.prototype.constructor

Hàm khởi tạo đã tạo ra đối tượng instance. Với các instance Intl.RelativeTimeFormat, giá trị khởi tạo là hàm khởi tạo Intl.RelativeTimeFormat.

Intl.RelativeTimeFormat.prototype[Symbol.toStringTag]

Giá trị khởi tạo của thuộc tính [Symbol.toStringTag] là chuỗi "Intl.RelativeTimeFormat". Thuộc tính này được dùng trong Object.prototype.toString().

Phương thức instance

Intl.RelativeTimeFormat.prototype.format()

Định dạng valueunit theo locale và tùy chọn định dạng của đối tượng Intl.RelativeTimeFormat đã cho.

Intl.RelativeTimeFormat.prototype.formatToParts()

Trả về một Array các đối tượng đại diện cho định dạng thời gian tương đối theo từng phần, có thể dùng để định dạng theo locale tùy chỉnh.

Intl.RelativeTimeFormat.prototype.resolvedOptions()

Trả về một đối tượng mới với các thuộc tính phản ánh locale và tùy chọn định dạng được tính toán trong quá trình khởi tạo đối tượng.

Ví dụ

Sử dụng format cơ bản

Ví dụ sau đây cho thấy cách sử dụng bộ định dạng thời gian tương đối cho ngôn ngữ tiếng Anh.

js
// Create a relative time formatter in your locale
// with default values explicitly passed in.
const rtf = new Intl.RelativeTimeFormat("en", {
  localeMatcher: "best fit", // other values: "lookup"
  numeric: "always", // other values: "auto"
  style: "long", // other values: "short" or "narrow"
});

// Format relative time using negative value (-1).
rtf.format(-1, "day"); // "1 day ago"

// Format relative time using positive value (1).
rtf.format(1, "day"); // "in 1 day"

Sử dụng formatToParts

Ví dụ sau đây cho thấy cách tạo bộ định dạng thời gian tương đối trả về các phần được định dạng.

js
const rtf = new Intl.RelativeTimeFormat("en", { numeric: "auto" });

// Format relative time using the day unit.
rtf.formatToParts(-1, "day");
// [{ type: "literal", value: "yesterday"}]

rtf.formatToParts(100, "day");
// [
//   { type: "literal", value: "in " },
//   { type: "integer", value: "100", unit: "day" },
//   { type: "literal", value: " days" }
// ]

Đặc tả kỹ thuật

Specification
ECMAScript® 2026 Internationalization API Specification
# relativetimeformat-objects

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

Xem thêm