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

瀏覽器相容性

另見