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()
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. |
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
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
|
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
|
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
|
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
|
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
|
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
|
完整支持
不支持
见实现注意事项。
Intl
Collator
DateTimeFormat
ListFormat
NumberFormat
PluralRules
RelativeTimeFormat
Object
Object.prototype.__defineGetter__()
Object.prototype.__defineSetter__()
Object.prototype.__lookupGetter__()
Object.prototype.__lookupSetter__()
Object.prototype.hasOwnProperty()
Object.prototype.isPrototypeOf()
Object.prototype.propertyIsEnumerable()
Object.prototype.toLocaleString()
Object.prototype.toSource()
Object.prototype.toString()
Object.prototype.valueOf()
Object.setPrototypeOf()