Intl.DateTimeFormat.prototype.resolvedOptions()
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"
語法
resolvedOptions()
引數
無。
返回值
一個新物件,其中包含此 DateTimeFormat 物件初始化期間計算出的選項。該物件具有以下屬性,按照列出的順序排列:
locale-
實際使用的區域設定的 BCP 47 語言標記,由 區域設定協商 過程確定。只有請求的
ca、hc和nuUnicode 擴充套件鍵才可能包含在輸出中。 calendar-
在
options引數中為此屬性提供的值,或使用 Unicode 擴充套件鍵"ca",並根據需要填充預設值。它是此區域設定支援的 日曆型別。預設值取決於區域設定。 numberingSystem-
在
options引數中為該屬性提供的值,或使用 Unicode 擴充套件鍵"nu",並根據需要填充預設值。它是此區域設定支援的 數字系統。預設值取決於區域設定。 timeZone-
在
options引數中為此屬性提供的值,並根據需要填充預設值。它是 IANA 時區名稱。預設值是執行時的預設時區。注意:
Temporal的標準化要求瀏覽器使用與原始指定相同的識別符號,而不是將其規範化為另一個別名。有關更多資訊,請參閱 時區和偏移量。 hourCycle可選-
在
options引數中為此屬性提供的值,或使用 Unicode 擴充套件鍵"hc",並根據需要填充預設值。如果options中提供了hour12,則它將覆蓋其他hourCycle設定。僅當已解析的選項還包括hour或timeStyle時才存在。它的值可以是"h11"、"h12"、"h23"或"h24"。預設值取決於區域設定,但"h24"永遠不會是預設值。 hour12可選-
從
hourCycle計算得出。僅當已解析的選項還包括hour或timeStyle時才存在。如果hourCycle是"h11"或"h12",則為true;如果hourCycle是"h23"或"h24",則為false。 weekday、era、year、month、day、dayPeriod、hour、minute、second、fractionalSecondDigits、timeZoneName可選-
在
options引數中的相應屬性與所選區域設定中可用的日期時間格式化組合和表示形式之間進行格式匹配所產生的值。其中某些屬性可能不存在,這表示相應的元件不會在格式化輸出中顯示。weekday、era和dayPeriod的值可以是"narrow"、"short"或"long";year、day、hour、minute和second的值可以是"numeric"、"2-digit"或"narrow";month的值可以是"numeric"、"2-digit"、"narrow"、"short"或"long";fractionalSecondDigits的值可以是1、2或3;timeZoneName的值可以是"short"、"long"、"shortOffset"、"longOffset"、"shortGeneric"或"longGeneric"。如果在
options中請求了這些屬性,則建構函式會阻止指定dateStyle和timeStyle,因此下面的組將永遠不會出現。 dateStyle、timeStyle可選-
在
options引數中為這些屬性提供的值。它們可以是"full"、"long"、"medium"、"short"或"none"。其中某些屬性可能不存在,這表示相應的元件不會在格式化輸出中顯示。如果在
options中請求了這些屬性,則建構函式會阻止指定單個日期時間元件選項,因此上述組將永遠不會出現。注意:儘管
dateStyle和timeStyle是單個日期和時間元件樣式的快捷方式,但它們解析到的確切(取決於區域設定的)元件樣式不包含在已解析的選項中。這確保了resolvedOptions()的結果可以直接傳遞給Intl.DateTimeFormat()建構函式(因為同時包含dateStyle或timeStyle和單個日期或時間元件樣式的options物件無效)。
示例
使用 resolvedOptions 方法
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() 來獲取使用者當前的預設時區以及區域設定偏好的日曆和數字系統。
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 |
瀏覽器相容性
載入中…