Intl.DateTimeFormat.prototype.resolvedOptions()

Baseline 已廣泛支援

此功能已成熟,可跨多種裝置和瀏覽器版本使用。自 2017 年 9 月以來,它已在瀏覽器中提供。

resolvedOptions() 方法是 Intl.DateTimeFormat 例項的一個方法,它返回一個新物件,該物件包含此 DateTimeFormat 物件初始化期間計算出的選項。

試一試

const region = new Intl.DateTimeFormat("zh-CN", { timeZone: "UTC" });
const options = region.resolvedOptions();

console.log(options.locale);
// Expected output: "zh-CN"

console.log(options.calendar);
// Expected output: "gregory"

console.log(options.numberingSystem);
// Expected output: "latn"

語法

js
resolvedOptions()

引數

無。

返回值

一個新物件,其中包含此 DateTimeFormat 物件初始化期間計算出的選項。該物件具有以下屬性,按照列出的順序排列:

locale

實際使用的區域設定的 BCP 47 語言標記,由 區域設定協商 過程確定。只有請求的 cahcnu Unicode 擴充套件鍵才可能包含在輸出中。

calendar

options 引數中為此屬性提供的值,或使用 Unicode 擴充套件鍵 "ca",並根據需要填充預設值。它是此區域設定支援的 日曆型別。預設值取決於區域設定。

numberingSystem

options 引數中為該屬性提供的值,或使用 Unicode 擴充套件鍵 "nu",並根據需要填充預設值。它是此區域設定支援的 數字系統。預設值取決於區域設定。

timeZone

options 引數中為此屬性提供的值,並根據需要填充預設值。它是 IANA 時區名稱。預設值是執行時的預設時區。

注意:Temporal 的標準化要求瀏覽器使用與原始指定相同的識別符號,而不是將其規範化為另一個別名。有關更多資訊,請參閱 時區和偏移量

hourCycle 可選

options 引數中為此屬性提供的值,或使用 Unicode 擴充套件鍵 "hc",並根據需要填充預設值。如果 options 中提供了 hour12,則它將覆蓋其他 hourCycle 設定。僅當已解析的選項還包括 hourtimeStyle 時才存在。它的值可以是 "h11""h12""h23""h24"。預設值取決於區域設定,但 "h24" 永遠不會是預設值。

hour12 可選

hourCycle 計算得出。僅當已解析的選項還包括 hourtimeStyle 時才存在。如果 hourCycle"h11""h12",則為 true;如果 hourCycle"h23""h24",則為 false

weekdayerayearmonthdaydayPeriodhourminutesecondfractionalSecondDigitstimeZoneName 可選

options 引數中的相應屬性與所選區域設定中可用的日期時間格式化組合和表示形式之間進行格式匹配所產生的值。其中某些屬性可能不存在,這表示相應的元件不會在格式化輸出中顯示。weekdayeradayPeriod 的值可以是 "narrow""short""long"yeardayhourminutesecond 的值可以是 "numeric""2-digit""narrow"month 的值可以是 "numeric""2-digit""narrow""short""long"fractionalSecondDigits 的值可以是 123timeZoneName 的值可以是 "short""long""shortOffset""longOffset""shortGeneric""longGeneric"

如果在 options 中請求了這些屬性,則建構函式會阻止指定 dateStyletimeStyle,因此下面的組將永遠不會出現。

dateStyletimeStyle 可選

options 引數中為這些屬性提供的值。它們可以是 "full""long""medium""short""none"。其中某些屬性可能不存在,這表示相應的元件不會在格式化輸出中顯示。

如果在 options 中請求了這些屬性,則建構函式會阻止指定單個日期時間元件選項,因此上述組將永遠不會出現。

注意:儘管 dateStyletimeStyle 是單個日期和時間元件樣式的快捷方式,但它們解析到的確切(取決於區域設定的)元件樣式不包含在已解析的選項中。這確保了 resolvedOptions() 的結果可以直接傳遞給 Intl.DateTimeFormat() 建構函式(因為同時包含 dateStyletimeStyle 和單個日期或時間元件樣式的 options 物件無效)。

示例

使用 resolvedOptions 方法

js
const germanFakeRegion = new Intl.DateTimeFormat("de-XX", { timeZone: "UTC" });
const usedOptions = germanFakeRegion.resolvedOptions();

usedOptions.locale; // "de" (because "de-XX" does not exist)
usedOptions.calendar; // "gregory"
usedOptions.numberingSystem; // "latn"
usedOptions.timeZone; // "UTC"
usedOptions.month; // "numeric"

獲取使用者的時區和區域設定偏好

不帶任何選項的 Intl.DateTimeFormat 建構函式會使用當前的系統設定。您可以使用 resolvedOptions() 來獲取使用者當前的預設時區以及區域設定偏好的日曆和數字系統。

js
const systemOptions = new Intl.DateTimeFormat().resolvedOptions();
systemOptions.timeZone; // e.g., "Europe/Brussels" or "Asia/Riyadh"
systemOptions.calendar; // e.g., "gregory" or "islamic-umalqura"
systemOptions.numberingSystem; // e.g., "latn" or "arab"
systemOptions.locale; // e.g., "nl-BE" or "ar-SA"

規範

規範
ECMAScript® 2026 國際化 API 規範
# sec-intl.datetimeformat.prototype.resolvedoptions

瀏覽器相容性

另見