该Intl.RelativeTimeFormat
对象是对象的构造函数,用于启用语言敏感的相对时间格式。
此交互式示例的源存储在GitHub存储库中。如果您想参与交互式示例项目,请克隆https://github.com/mdn/interactive-examples并向我们发送拉取请求。
句法
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
实例从其原型继承以下属性:
-
Intl.RelativeTimeFormat.prototype.constructor
-
A reference to
Intl.RelativeTimeFormat
.
方法
RelativeTimeFormat
实例从其原型继承以下方法:
-
Intl.RelativeTimeFormat.prototype.format()
-
Formats a
value
and aunit
according to the locale and formatting options of the givenIntl.RelativeTimeFormat
object. -
Intl.RelativeTimeFormat.prototype.formatToParts()
-
Returns an
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
用法
以下示例显示如何使用英语创建相对时间格式化程序。
//在语言环境中创建相对时间格式化程序 //显式传入默认值。 const rtf = new Intl.RelativeTimeFormat("en",{ localeMatcher:"bestfit",//其他值:"查找" numeric:"always",//其他值:"auto" style:"long",//其他值:"短"或"窄" }); //使用负值(-1)格式化相对时间。 rtf.format(-1,"day"); //>"1 day ago" //使用正值(1)格式化相对时间。 rtf.format(1,"day"); //>"in 1 day"
使用auto
选项
如果numeric:auto
选项被传递,它将生成字符串yesterday
或tomorrow
代替1 day ago
或in 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
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.
Update compatibility data on GitHub
Desktop | Mobile | Server | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
RelativeTimeFormat |
Chrome Full support 71 | Edge No support No | Firefox Full support 65 | IE No support No | Opera Full support 58 | Safari No support No | WebView Android Full support 71 | Chrome Android Full support 71 | Firefox Android Full support 65 | Opera Android Full support 50 | Safari iOS No support No | Samsung Internet Android Full support 10.0 | nodejs Full support 12.0.0 |
RelativeTimeFormat() constructor |
Chrome Full support 71 | Edge No support No | Firefox Full support 65 | IE No support No | Opera Full support 58 | Safari No support No | WebView Android Full support 71 | Chrome Android Full support 71 | Firefox Android Full support 65 | Opera Android Full support 50 | Safari iOS No support No | Samsung Internet Android Full support 10.0 | nodejs Full support 12.0.0 |
format |
Chrome Full support 71 | Edge No support No | Firefox Full support 65 | IE No support No | Opera Full support 58 | Safari No support No | WebView Android Full support 71 | Chrome Android Full support 71 | Firefox Android Full support 65 | Opera Android Full support 50 | Safari iOS No support No | Samsung Internet Android Full support 10.0 | nodejs Full support 12.0.0 |
formatToParts |
Chrome Full support 71 | Edge No support No | Firefox Full support 70 | IE No support No | Opera Full support 58 | Safari No support No | WebView Android Full support 71 | Chrome Android Full support 71 | Firefox Android No support No | Opera Android Full support 50 | Safari iOS No support No | Samsung Internet Android Full support 10.0 | nodejs Full support 12.0.0 |
prototype |
Chrome Full support 71 | Edge No support No | Firefox Full support 65 | IE No support No | Opera Full support 58 | Safari No support No | WebView Android Full support 71 | Chrome Android Full support 71 | Firefox Android Full support 65 | Opera Android Full support 50 | Safari iOS No support No | Samsung Internet Android Full support 10.0 | nodejs Full support 12.0.0 |
resolvedOptions |
Chrome Full support 71 | Edge No support No | Firefox Full support 65 | IE No support No | Opera Full support 58 | Safari No support No | WebView Android Full support 71 | Chrome Android Full support 71 | Firefox Android Full support 65 | Opera Android Full support 50 | Safari iOS No support No | Samsung Internet Android Full support 10.0 | nodejs Full support 12.0.0 |
supportedLocalesOf |
Chrome Full support 71 | Edge No support No | Firefox Full support 65 | IE No support No | Opera Full support 58 | Safari No support No | WebView Android Full support 71 | Chrome Android Full support 71 | Firefox Android Full support 65 | Opera Android Full support 50 | Safari iOS No support No | Samsung Internet Android Full support 10.0 | nodejs Full support 12.0.0 |
Legend
- Full support
- Full support
- No support
- No support