Intl.DurationFormat() constructor
Baseline
2025
Newly available
Since March 2025, this feature works across the latest devices and browser versions. This feature might not work in older devices or browsers.
The Intl.DurationFormat() constructor creates Intl.DurationFormat objects.
Syntax
new Intl.DurationFormat()
new Intl.DurationFormat(locales)
new Intl.DurationFormat(locales, options)
Parameters
localesOptional-
A string with a BCP 47 language tag or an
Intl.Localeinstance, or an array of such locale identifiers. The runtime's default locale is used whenundefinedis passed or when none of the specified locale identifiers is supported. For the general form and interpretation of thelocalesargument, see the parameter description on theIntlmain page.The following Unicode extension key is allowed:
nu-
See
numberingSystem.
This key can also be set with
options(as listed below). When both are set, theoptionsproperty takes precedence. optionsOptional-
An object containing the following properties, in the order they are retrieved (all of them are optional):
localeMatcher-
The locale matching algorithm to use. Possible values are
"lookup"and"best fit"; the default is"best fit". For information about this option, see Locale identification and negotiation. numberingSystem-
The numbering system to use for number formatting, such as
"arab","hans","mathsans", and so on. For a list of supported numbering system types, seeIntl.supportedValuesOf(); the default is locale dependent. This option can also be set through thenuUnicode extension key; if both are provided, thisoptionsproperty takes precedence. style-
The style of the formatted duration. This value is used as the default for all other unit options, and also corresponds to the
styleoption ofIntl.ListFormat()when concatenating the list of duration units. Possible values are:"long"-
E.g., 1 hour and 50 minutes
"short"(default)-
E.g., 1 hr, 50 min
"narrow"-
E.g., 1h 50m
"digital"-
E.g., 1:50:00
years-
The style of the formatted years. Possible values are
"long","short", and"narrow"; the default isoptions.styleif it's not"digital", and"short"otherwise. yearsDisplay-
Whether to always display years, or only if nonzero. Possible values are
"always"and"auto"; the default is"auto"ifyearsis unspecified, and"always"otherwise. months-
The style of the formatted months. Possible values are
"long","short", and"narrow"; the default isoptions.styleif it's not"digital", and"short"otherwise. monthsDisplay-
Whether to always display months, or only if nonzero. Possible values are
"always"and"auto"; the default is"auto"ifmonthsis unspecified, and"always"otherwise. weeks-
The style of the formatted weeks. Possible values are
"long","short", and"narrow"; the default isoptions.styleif it's not"digital", and"short"otherwise. weeksDisplay-
Whether to always display weeks, or only if nonzero. Possible values are
"always"and"auto"; the default is"auto"ifweeksis unspecified, and"always"otherwise. days-
The style of the formatted days. Possible values are
"long","short", and"narrow"; the default isoptions.styleif it's not"digital", and"short"otherwise. daysDisplay-
Whether to always display days, or only if nonzero. Possible values are
"always"and"auto"; the default is"auto"ifdaysis unspecified, and"always"otherwise. hours-
The style of the formatted hours. Possible values are
"long","short","narrow","numeric", and"2-digit"; the default isoptions.styleif it's not"digital", and"numeric"otherwise. hoursDisplay-
Whether to always display hours, or only if nonzero. Possible values are
"always"and"auto"; the default is"auto"ifhoursis unspecified andoptions.styleis not"digital", and"always"otherwise. minutes-
The style of the formatted minutes.
- If
hoursis"numeric"or"2-digit", possible values are"numeric"and"2-digit", and"numeric"is normalized to"2-digit"; the default is"numeric". - Otherwise, possible values are
"long","short","narrow","numeric", and"2-digit"; the default isoptions.styleif it's not"digital", and"numeric"otherwise.
- If
minutesDisplay-
Whether to always display minutes, or only if nonzero. Possible values are
"always"and"auto"; the default is"auto"ifminutesis unspecified andoptions.styleis not"digital", and"always"otherwise. seconds-
The style of the formatted seconds.
- If
minutesis"numeric"or"2-digit", possible values are"numeric"and"2-digit", and"numeric"is normalized to"2-digit"; the default is"numeric". - Otherwise, possible values are
"long","short","narrow","numeric", and"2-digit"; the default isoptions.styleif it's not"digital", and"numeric"otherwise.
- If
secondsDisplay-
Whether to always display seconds, or only if nonzero. Possible values are
"always"and"auto"; the default is"auto"ifsecondsis unspecified andoptions.styleis not"digital", and"always"otherwise. milliseconds-
The style of the formatted milliseconds.
- If
secondsis"numeric"or"2-digit", the only possible value is"numeric"; the default is"numeric". - Otherwise, possible values are
"long","short","narrow", and"numeric"; the default isoptions.styleif it's not"digital", and"numeric"otherwise.
- If
millisecondsDisplay-
Whether to always display milliseconds, or only if nonzero.
- If
secondsis"numeric"or"2-digit", the only possible value is"auto"; the default is only"auto"whenmillisecondsis unspecified. - Otherwise, possible values are
"always"and"auto"; the default is"auto"ifmillisecondsis unspecified, and"always"otherwise.
- If
microseconds-
The style of the formatted microseconds.
- If
millisecondsis"numeric", the only possible value is"numeric"; the default is"numeric". - Otherwise, possible values are
"long","short","narrow", and"numeric"; the default isoptions.styleif it's not"digital", and"numeric"otherwise.
- If
microsecondsDisplay-
Whether to always display microseconds, or only if nonzero.
- If
millisecondsis"numeric", the only possible value is"auto"; the default is only"auto"whenmicrosecondsis unspecified. - Otherwise, possible values are
"always"and"auto"; the default is"auto"ifmicrosecondsis unspecified, and"always"otherwise.
- If
nanoseconds-
The style of the formatted nanoseconds.
- If
microsecondsis"numeric", the only possible value is"numeric"; the default is"numeric". - Otherwise, possible values are
"long","short","narrow", and"numeric"; the default isoptions.styleif it's not"digital", and"numeric"otherwise.
- If
nanosecondsDisplay-
Whether to always display nanoseconds, or only if nonzero.
- If
microsecondsis"numeric", the only possible value is"auto"; the default is only"auto"whennanosecondsis unspecified. - Otherwise, possible values are
"always"and"auto"; the default is"auto"ifnanosecondsis unspecified, and"always"otherwise.
- If
fractionalDigits-
Number of how many fractional second digits to display in the output. Possible values are from
0to9; the default isundefined(include as many fractional digits as necessary).
Exceptions
RangeError-
Thrown if
localesoroptionscontain invalid values.
Description
For each time segment, an Intl.NumberFormat object is constructed under the hood. It uses the following options (see Intl.NumberFormat() for details):
numberingSystem: the value ofoptions.numberingSystem
When milliseconds, microseconds, or nanoseconds uses the "numeric" style, the following options are also used:
minimumFractionDigits:0whenoptions.fractionalDigitsisundefined,options.fractionalDigitsotherwisemaximumFractionDigits:9whenoptions.fractionalDigitsisundefined,options.fractionalDigitsotherwiseroundingMode:"trunc"
When the time segment uses the "2-digit" style, the following options are also used:
minimumIntegerDigits:2
When the time segment uses the "long", "short", or "narrow" style, the following options are also used:
style:"unit"when"long","short", or"narrow"is specified,undefinedotherwiseunit: the currently formatted unit ("years","days","nanoseconds", etc.)unitDisplay: the value of the time segment style ("long","short", or"narrow")
Examples
>Using the Intl.DurationFormat() constructor
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"
Specifications
| Specification |
|---|
| Intl.DurationFormat> # sec-intl-durationformat-constructor> |
Browser compatibility
Loading…