語法
new Intl.DurationFormat()
new Intl.DurationFormat(locales)
new Intl.DurationFormat(locales, options)
引數
locales可選-
包含 BCP 47 語言標籤的字串或
Intl.Locale例項,或此類區域設定識別符號的陣列。當傳入undefined或未指定支援的區域設定識別符號時,將使用執行時的預設區域設定。有關locales引數的一般形式和解釋,請參閱Intl主頁上的引數描述。允許使用以下 Unicode 擴充套件鍵
nu-
請參閱
numberingSystem。
此鍵也可以透過
options(如下所列)設定。當兩者都設定時,options屬性優先。 options可選-
一個包含以下屬性的物件,按照它們被檢索的順序排列(所有屬性都是可選的)
localeMatcher-
要使用的區域設定匹配演算法。可能的值是
"lookup"和"best fit";預設值為"best fit"。有關此選項的資訊,請參閱 區域設定標識和協商。 numberingSystem-
用於數字格式化的編號系統,例如
"arab"、"hans"、"mathsans"等。有關支援的編號系統型別的列表,請參閱Intl.supportedValuesOf();預設值取決於區域設定。此選項也可以透過nuUnicode 擴充套件鍵設定;如果兩者都提供,則此options屬性優先。 style-
格式化持續時間時使用的樣式。此值用作所有其他單位選項的預設值,並且在連線持續時間單位列表時也對應於
Intl.ListFormat()的style選項。可能的值有:"long"-
例如,1 小時 50 分鐘
"short"(預設)-
例如,1 小時,50 分鐘
"narrow"-
例如,1h 50m
"digital"-
例如,1:50:00
years-
格式化年數時使用的樣式。可能的值為
"long"、"short"和"narrow";預設值為options.style(如果不是"digital"),否則為"short"。 yearsDisplay-
是否始終顯示年數,或僅在非零時顯示。可能的值為
"always"和"auto";如果未指定years,則預設為"auto",否則預設為"always"。 months-
格式化月數時使用的樣式。可能的值為
"long"、"short"和"narrow";預設值為options.style(如果不是"digital"),否則為"short"。 monthsDisplay-
是否始終顯示月數,或僅在非零時顯示。可能的值為
"always"和"auto";如果未指定months,則預設為"auto",否則預設為"always"。 weeks-
格式化週數時使用的樣式。可能的值為
"long"、"short"和"narrow";預設值為options.style(如果不是"digital"),否則為"short"。 weeksDisplay-
是否始終顯示週數,或僅在非零時顯示。可能的值為
"always"和"auto";如果未指定weeks,則預設為"auto",否則預設為"always"。 days-
格式化天數時使用的樣式。可能的值為
"long"、"short"和"narrow";預設值為options.style(如果不是"digital"),否則為"short"。 daysDisplay-
是否始終顯示天數,或僅在非零時顯示。可能的值為
"always"和"auto";如果未指定days,則預設為"auto",否則預設為"always"。 hours-
格式化小時數時使用的樣式。可能的值為
"long"、"short"、"narrow"、"numeric"和"2-digit";預設值為options.style(如果不是"digital"),否則為"numeric"。 hoursDisplay-
是否始終顯示小時數,或僅在非零時顯示。可能的值為
"always"和"auto";如果未指定hours且options.style不是"digital",則預設為"auto",否則預設為"always"。 minutes-
格式化分鐘數時使用的樣式。
- 如果
hours為"numeric"或"2-digit",可能的值為"numeric"和"2-digit",並且"numeric"會被標準化為"2-digit";預設值為"numeric"。 - 否則,可能的值為
"long"、"short"、"narrow"、"numeric"和"2-digit";預設值為options.style(如果不是"digital"),否則為"numeric"。
- 如果
minutesDisplay-
是否始終顯示分鐘數,或僅在非零時顯示。可能的值為
"always"和"auto";如果未指定minutes且options.style不是"digital",則預設為"auto",否則預設為"always"。 seconds-
格式化秒數時使用的樣式。
- 如果
minutes為"numeric"或"2-digit",可能的值為"numeric"和"2-digit",並且"numeric"會被標準化為"2-digit";預設值為"numeric"。 - 否則,可能的值為
"long"、"short"、"narrow"、"numeric"和"2-digit";預設值為options.style(如果不是"digital"),否則為"numeric"。
- 如果
secondsDisplay-
是否始終顯示秒數,或僅在非零時顯示。可能的值為
"always"和"auto";如果未指定seconds且options.style不是"digital",則預設為"auto",否則預設為"always"。 milliseconds-
格式化毫秒時使用的樣式。
- 如果
seconds為"numeric"或"2-digit",唯一可能的值是"numeric";預設值為"numeric"。 - 否則,可能的值為
"long"、"short"、"narrow"和"numeric";預設值為options.style(如果不是"digital"),否則為"numeric"。
- 如果
millisecondsDisplay-
是否始終顯示毫秒,或僅在非零時顯示。
- 如果
seconds為"numeric"或"2-digit",唯一可能的值是"auto";只有當milliseconds未指定時,預設值為"auto"。 - 否則,可能的值為
"always"和"auto";如果未指定milliseconds,則預設為"auto",否則預設為"always"。
- 如果
microseconds-
格式化微秒時使用的樣式。
- 如果
milliseconds為"numeric",唯一可能的值是"numeric";預設值為"numeric"。 - 否則,可能的值為
"long"、"short"、"narrow"和"numeric";預設值為options.style(如果不是"digital"),否則為"numeric"。
- 如果
microsecondsDisplay-
是否始終顯示微秒,或僅在非零時顯示。
- 如果
milliseconds為"numeric",唯一可能的值是"auto";只有當microseconds未指定時,預設值為"auto"。 - 否則,可能的值為
"always"和"auto";如果未指定microseconds,則預設為"auto",否則預設為"always"。
- 如果
nanoseconds-
格式化納秒時使用的樣式。
- 如果
microseconds為"numeric",唯一可能的值是"numeric";預設值為"numeric"。 - 否則,可能的值為
"long"、"short"、"narrow"和"numeric";預設值為options.style(如果不是"digital"),否則為"numeric"。
- 如果
nanosecondsDisplay-
是否始終顯示納秒,或僅在非零時顯示。
- 如果
microseconds為"numeric",唯一可能的值是"auto";只有當nanoseconds未指定時,預設值為"auto"。 - 否則,可能的值為
"always"和"auto";如果未指定nanoseconds,則預設為"auto",否則預設為"always"。
- 如果
fractionalDigits-
要在輸出中顯示的秒的小數位數。可能的值為
0到9;預設值為undefined(包含必要的小數位數)。
異常
RangeError-
如果
locales或options包含無效值,則丟擲此錯誤。
描述
對於每個時間段,後臺都會構造一個 Intl.NumberFormat 物件。它使用以下選項(有關詳細資訊,請參閱 Intl.NumberFormat())
numberingSystem:options.numberingSystem的值
當 milliseconds、microseconds 或 nanoseconds 使用 "numeric" 樣式時,還將使用以下選項:
minimumFractionDigits:當options.fractionalDigits為undefined時為0,否則為options.fractionalDigitsmaximumFractionDigits:當options.fractionalDigits為undefined時為9,否則為options.fractionalDigitsroundingMode:"trunc"
當時間段使用 "2-digit" 樣式時,還將使用以下選項:
minimumIntegerDigits:2
當時間段使用 "long"、"short" 或 "narrow" 樣式時,還將使用以下選項:
style:如果指定了"long"、"short"或"narrow",則為"unit",否則為undefinedunit:當前格式化的單位("years"、"days"、"nanoseconds"等)unitDisplay:時間段樣式的("long"、"short"或"narrow")值
示例
使用 Intl.DurationFormat() 建構函式
const duration = {
hours: 2,
minutes: 20,
seconds: 35,
};
console.log(new Intl.DurationFormat("pt", { style: "long" }).format(duration));
// "2 horas, 20 minutos e 35 segundos"
規範
| 規範 |
|---|
| Intl.DurationFormat # sec-intl-durationformat-constructor |
瀏覽器相容性
載入中…