Intl.RelativeTimeFormat

Intl.RelativeTimeFormat对象启用本地化的相对时间格式。

句法

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

参数

locales

可选的。带有BCP 47语言标记的字符串,或此类字符串的数组。有关参数的一般形式和解释locales,请参阅Intl page

options
可选的。具有以下部分或全部属性的对象:
  • localeMatcher
    要使用的区域设置匹配算法。可能的值是"lookup""best fit"; 默认是"best fit"。有关此选项的信息,请参阅Intl
  • numeric
    输出消息的格式。可能的值是:
    • "always"(默认,例如,1 day ago),
    • "auto"(例如yesterday)。该"auto"值允许不必总是在输出中使用数值。
  • style
    国际化信息的长度。可能的值是:
    • "long"(默认,例如,in 1 month)
    • "short"(例如in 1 mo.),
    • "narrow"(例如in 1 mo.)。狭窄的风格可能类似于某些语言环境的短风格。

描述

属性

Intl.RelativeTimeFormat.prototype
允许向所有对象添加属性。

方法

Intl.RelativeTimeFormat.supportedLocalesOf()
返回一个数组,其中包含所支持的语言环境,而不必回退到运行时的默认语言环境。

RelativeTimeFormat 实例

属性

RelativeTimeFormat 实例从其原型继承以下属性:

方法

RelativeTimeFormat 实例从其原型继承以下方法:

例子

基本format用法

以下示例显示如何使用英语创建相对时间格式化程序。

//在语言环境中创建相对时间格式化程序
//显式传入默认值。
const rtf = new Intl.RelativeTimeFormat("en",{
    localeMatcher: "bestfit",//其他值:"lookup"
    numeric: "always",//其他值:"auto"
    style: "long",//其他值:"short"或"narrow"
});

//使用负值(-1)格式化相对时间。
rtf.format(-1,"day");
//>"1 day ago"

//使用正值(1)格式化相对时间。
rtf.format(1,"day");
//>"in 1 day"

使用auto选项

如果numeric:auto选项被传递,它将生成字符串yesterdaytomorrow代替1 day agoin 1 day。这允许不必总是在输出中使用数值。

//在语言环境中创建相对时间格式化程序
//使用数字:传入"auto"选项值。
const rtf = new Intl.RelativeTimeFormat("en",{numeric: "auto"});

//使用负值(-1)格式化相对时间。
rtf.format(-1,"day");
//>"yesterday"

//使用正日单位(1)格式化相对时间。
rtf.format(1,"day");
//>"tomorrow"

运用 formatToParts

以下示例显示如何创建返回格式化部件的相对时间格式器

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

//使用日期单位格式化相对时间。
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"}]

Specifications

Specification Status Comment
Intl.RelativeTimeFormat Constructor Stage 3

Browser compatibility

Update compatibility data on GitHub
DesktopMobileServer
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung InternetNode.js
RelativeTimeFormatChrome Full support 71Edge Full support 79Firefox Full support 65IE No support NoOpera Full support 58Safari Full support 14WebView Android Full support 71Chrome Android Full support 71Firefox Android Full support 65Opera Android Full support 50Safari iOS Full support 14Samsung Internet Android Full support 10.0nodejs Full support 12.0.0
Notes
Full support 12.0.0
Notes
Notes Before version 13.0.0, only the locale data for en-US is available by default. See the RelativeTimeFormat() constructor for more details.
RelativeTimeFormat() constructorChrome Full support 71Edge Full support 79Firefox Full support 65IE No support NoOpera Full support 58Safari Full support 14WebView Android Full support 71Chrome Android Full support 71Firefox Android Full support 65Opera Android Full support 50Safari iOS Full support 14Samsung Internet Android Full support 10.0nodejs Full support 13.0.0
Full support 13.0.0
Partial support 12.0.0
Notes
Notes 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.
formatChrome Full support 71Edge Full support 79Firefox Full support 65IE No support NoOpera Full support 58Safari Full support 14WebView Android Full support 71Chrome Android Full support 71Firefox Android Full support 65Opera Android Full support 50Safari iOS Full support 14Samsung Internet Android Full support 10.0nodejs Full support 12.0.0
Notes
Full support 12.0.0
Notes
Notes Before version 13.0.0, only the locale data for en-US is available by default. See the RelativeTimeFormat() constructor for more details.
formatToPartsChrome Full support 71Edge Full support 79Firefox Full support 70IE No support NoOpera Full support 58Safari Full support 14WebView Android Full support 71Chrome Android Full support 71Firefox Android No support NoOpera Android Full support 50Safari iOS Full support 14Samsung Internet Android Full support 10.0nodejs Full support 12.0.0
Notes
Full support 12.0.0
Notes
Notes Before version 13.0.0, only the locale data for en-US is available by default. See the RelativeTimeFormat() constructor for more details.
resolvedOptionsChrome Full support 71Edge Full support 79Firefox Full support 65IE No support NoOpera Full support 58Safari Full support 14WebView Android Full support 71Chrome Android Full support 71Firefox Android Full support 65Opera Android Full support 50Safari iOS Full support 14Samsung Internet Android Full support 10.0nodejs Full support 12.0.0
Notes
Full support 12.0.0
Notes
Notes Before version 13.0.0, only the locale data for en-US is available by default. See the RelativeTimeFormat() constructor for more details.
supportedLocalesOfChrome Full support 71Edge Full support 79Firefox Full support 65IE No support NoOpera Full support 58Safari Full support 14WebView Android Full support 71Chrome Android Full support 71Firefox Android Full support 65Opera Android Full support 50Safari iOS Full support 14Samsung Internet Android Full support 10.0nodejs Full support 13.0.0
Full support 13.0.0
Partial support 12.0.0
Notes
Notes 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.

Legend

Full support  
Full support
No support  
No support
See implementation notes.
See implementation notes.

See also