Intl.RelativeTimeFormat() constructor creates Intl.RelativeTimeFormat 对象。

句法

new Intl.RelativeTimeFormat([locales[, options]])

					

参数

locales

Optional. A string with a BCP 47 language tag, or an array of such strings. For the general form and interpretation of the locales argument, see the Intl page .

options
Optional. An object with some or all of the following properties:
  • localeMatcher
    The locale matching algorithm to use. Possible values are "lookup" and "best fit" ; the default is "best fit" . For information about this option, see Intl .
  • numeric
    The format of output message. Possible values are:
    • "always" (default, e.g., 1 day ago ),
    • or "auto" (e.g., yesterday ). The "auto" value allows to not always have to use numeric values in the output.
  • style
    The length of the internationalized message. Possible values are:
    • "long" (default, e.g., in 1 month )
    • "short" (e.g., in 1 mo. ),
    • or "narrow" (e.g., in 1 mo. ). The narrow style could be similar to the short style for some locales.

范例

基本 format usage

The following example shows how to create a relative time formatter using 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"
					

使用 auto option

numeric:auto option is passed, it will produce the string yesterday or tomorrow 而不是 1 day ago or in 1 day . This allows to not always have to use numeric values in the output.

// Create a relative time formatter in your locale
// with numeric: "auto" option value passed in.
const rtf = new Intl.RelativeTimeFormat("en", { numeric: "auto" });
// Format relative time using negative value (-1).
rtf.format(-1, "day");
// > "yesterday"
// Format relative time using positive day unit (1).
rtf.format(1, "day");
// > "tomorrow"
					

规范

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

浏览器兼容性

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 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.

图例

完整支持

完整支持

不支持

不支持

见实现注意事项。

另请参阅

元数据

  • 最后修改: