Intl.RelativeTimeFormat object enables language-sensitive relative time formatting.

The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.

构造函数

Intl.RelativeTimeFormat.RelativeTimeFormat()
创建新的 Intl.RelativeTimeFormat 对象。

静态方法

Intl.RelativeTimeFormat.supportedLocalesOf()

Returns an array containing those of the provided locales that are supported without having to fall back to the runtime's default locale.

实例方法

Intl.RelativeTimeFormat.prototype.format()
Formats a value unit according to the locale and formatting options of the given Intl.RelativeTimeFormat 对象。
Intl.RelativeTimeFormat.prototype.formatToParts()
返回 Array of objects representing the relative time format in parts that can be used for custom locale-aware formatting.
Intl.RelativeTimeFormat.prototype.resolvedOptions()

Returns a new object with properties reflecting the locale and formatting options computed during initialization of the object.

范例

基本 format usage

The following example shows how to use a relative time formatter for the English language.

// 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"
					

使用 formatToParts

The following example shows how to create a relative time formatter returning formatted parts

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" }]
					

规范

规范 状态 注释
ECMAScript 国际化 API (ECMA-402)
The definition of 'RelativeTimeFormat' in that specification.

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
Desktop Mobile Server
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet Node.js
RelativeTimeFormat Chrome 71 Edge 79 Firefox 65 IE No Opera 58 Safari No WebView Android 71 Chrome Android 71 Firefox Android 65 Opera Android 50 Safari iOS No Samsung Internet Android 10.0 nodejs 12.0.0
12.0.0
Before version 13.0.0, only the locale data for en-US is available by default. See the RelativeTimeFormat() 构造函数 了解更多细节。
RelativeTimeFormat() 构造函数 Chrome 71 Edge 79 Firefox 65 IE No Opera 58 Safari No WebView Android 71 Chrome Android 71 Firefox Android 65 Opera Android 50 Safari iOS No Samsung Internet Android 10.0 nodejs 13.0.0
13.0.0
部分支持 12.0.0
Before version 13.0.0, only the locale data for en-US is available by default. When other locales are specified, the RelativeTimeFormat instance silently falls back to en-US . To make full ICU (locale) data available for versions prior to 13, see Node.js documentation on the --with-intl option and how to provide the data.
format Chrome 71 Edge 79 Firefox 65 IE No Opera 58 Safari No WebView Android 71 Chrome Android 71 Firefox Android 65 Opera Android 50 Safari iOS No Samsung Internet Android 10.0 nodejs 12.0.0
12.0.0
Before version 13.0.0, only the locale data for en-US is available by default. See the RelativeTimeFormat() 构造函数 了解更多细节。
formatToParts Chrome 71 Edge 79 Firefox 70 IE No Opera 58 Safari No WebView Android 71 Chrome Android 71 Firefox Android No Opera Android 50 Safari iOS No Samsung Internet Android 10.0 nodejs 12.0.0
12.0.0
Before version 13.0.0, only the locale data for en-US is available by default. See the RelativeTimeFormat() 构造函数 了解更多细节。
resolvedOptions Chrome 71 Edge 79 Firefox 65 IE No Opera 58 Safari No WebView Android 71 Chrome Android 71 Firefox Android 65 Opera Android 50 Safari iOS No Samsung Internet Android 10.0 nodejs 12.0.0
12.0.0
Before version 13.0.0, only the locale data for en-US is available by default. See the RelativeTimeFormat() 构造函数 了解更多细节。
supportedLocalesOf Chrome 71 Edge 79 Firefox 65 IE No Opera 58 Safari No WebView Android 71 Chrome Android 71 Firefox Android 65 Opera Android 50 Safari iOS No Samsung Internet Android 10.0 nodejs 13.0.0
13.0.0
部分支持 12.0.0
Before version 13.0.0, only the locale data for en-US is available by default. To make full ICU (locale) data available for versions prior to 13, see Node.js documentation on the --with-intl option and how to provide the data.

图例

完整支持

完整支持

不支持

不支持

见实现注意事项。

另请参阅

元数据

  • 最后修改:

Copyright  © 2014-2026 乐数软件    

工业和信息化部: 粤ICP备14079481号-1