Intl.RelativeTimeFormat.prototype.resolvedOptions()

Baseline 已广泛支持

此功能已成熟,并可在许多设备和浏览器版本上使用。自 2020 年 9 月起,所有浏览器均已提供此功能。

resolvedOptions() 方法是 Intl.RelativeTimeFormat 实例上的一个方法,它返回一个新的对象,该对象的属性反映了在此 RelativeTimeFormat 对象初始化期间计算的选项。

试一试

const rtf1 = new Intl.RelativeTimeFormat("en", { style: "narrow" });
const options1 = rtf1.resolvedOptions();

const rtf2 = new Intl.RelativeTimeFormat("es", { numeric: "auto" });
const options2 = rtf2.resolvedOptions();

console.log(`${options1.locale}, ${options1.style}, ${options1.numeric}`);
// Expected output: "en, narrow, always"

console.log(`${options2.locale}, ${options2.style}, ${options2.numeric}`);
// Expected output: "es, long, auto"

语法

js
resolvedOptions()

参数

无。

返回值

一个新的对象,其属性反映了在此 RelativeTimeFormat 对象初始化期间计算的选项。该对象具有以下属性,按照列出的顺序排列

locale

通过 区域设置协商 过程确定的实际使用的区域设置的 BCP 47 语言标签。输出中可能只包含请求的 nu Unicode 扩展键(如果已请求)。

style

options 参数中为此属性提供的值,根据需要填充了默认值。它可以是 "long""short""narrow"。默认值为 "long"

numeric

options 参数中为此属性提供的值,根据需要填充了默认值。它可以是 "always""auto"。默认值为 "always"

numberingSystem

options 参数中为该属性提供的值,或使用 Unicode 扩展键 "nu",并根据需要填充默认值。它是此区域设置支持的 数字系统。默认值取决于区域设置。

示例

使用 resolvedOptions() 方法

js
const de = new Intl.RelativeTimeFormat("de-DE");
const usedOptions = de.resolvedOptions();

usedOptions.locale; // "de-DE"
usedOptions.style; // "long"
usedOptions.numeric; // "always"
usedOptions.numberingSystem; // "latn"

规范

规范
ECMAScript® 2026 国际化 API 规范
# sec-intl.relativetimeformat.prototype.resolvedoptions

浏览器兼容性

另见