The Intl.DurationFormat()
constructor creates Intl.DurationFormat objects.
Syntax
new Intl.DurationFormat()
new Intl.DurationFormat(locales)
new Intl.DurationFormat(locales, options)
Note:
Intl.DurationFormat()
can only be constructed withnew
. Attempting to call it withoutnew
throws a TypeError.
Parameters
locales
: A string with a BCP 47 language tag or an Intl.Locale instance, or an array of such locale identifiers. For the general form and interpretation of the
locales
argument, see the parameter description on theIntl
main page.The following Unicode extension key is allowed:
nu
- : See
numberingSystem
.
- : See
This key can also be set with
options
(as listed below). When both are set, theoptions
property takes precedence.
options
: 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.
- : The locale matching algorithm to use. Possible values are
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.Locale.prototype.getNumberingSystems()
. This option can also be set through thenu
Unicode extension key; if both are provided, thisoptions
property takes precedence.
- : The numbering system to use for number formatting, such as
style
- : The style of the formatted duration. 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
- : The style of the formatted duration. Possible values are:
years
- : The style of the formatted years. Possible values are
"long"
,"short"
, and"narrow"
; the default isoptions.style
if it's not"digital"
, and"short"
otherwise.
- : The style of the formatted years. Possible values are
yearsDisplay
- : Whether to always display years, or only if nonzero. Possible values are
"always"
and"auto"
; the default is"auto"
ifyears
is unspecified, and"always"
otherwise.
- : Whether to always display years, or only if nonzero. Possible values are
months
- : The style of the formatted months. Possible values are
"long"
,"short"
, and"narrow"
; the default isoptions.style
if it's not"digital"
, and"short"
otherwise.
- : The style of the formatted months. Possible values are
monthsDisplay
- : Whether to always display months, or only if nonzero. Possible values are
"always"
and"auto"
; the default is"auto"
ifmonths
is unspecified, and"always"
otherwise.
- : Whether to always display months, or only if nonzero. Possible values are
weeks
- : The style of the formatted weeks. Possible values are
"long"
,"short"
, and"narrow"
; the default isoptions.style
if it's not"digital"
, and"short"
otherwise.
- : The style of the formatted weeks. Possible values are
weeksDisplay
- : Whether to always display weeks, or only if nonzero. Possible values are
"always"
and"auto"
; the default is"auto"
ifweeks
is unspecified, and"always"
otherwise.
- : Whether to always display weeks, or only if nonzero. Possible values are
days
- : The style of the formatted days. Possible values are
"long"
,"short"
, and"narrow"
; the default isoptions.style
if it's not"digital"
, and"short"
otherwise.
- : The style of the formatted days. Possible values are
daysDisplay
- : Whether to always display days, or only if nonzero. Possible values are
"always"
and"auto"
; the default is"auto"
ifdays
is unspecified, and"always"
otherwise.
- : Whether to always display days, or only if nonzero. Possible values are
hours
- : The style of the formatted hours. Possible values are
"long"
,"short"
,"narrow"
,"numeric"
, and"2-digit"
; the default isoptions.style
if it's not"digital"
, and"numeric"
otherwise.
- : The style of the formatted hours. Possible values are
hoursDisplay
- : Whether to always display hours, or only if nonzero. Possible values are
"always"
and"auto"
; the default is"auto"
ifhours
is unspecified andoptions.style
is not"digital"
, and"always"
otherwise.
- : Whether to always display hours, or only if nonzero. Possible values are
minutes
- : The style of the formatted minutes.
- If
hours
is"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.style
if it's not"digital"
, and"numeric"
otherwise.
- If
- : The style of the formatted minutes.
minutesDisplay
- : Whether to always display minutes, or only if nonzero. Possible values are
"always"
and"auto"
; the default is"auto"
ifminutes
is unspecified andoptions.style
is not"digital"
, and"always"
otherwise.
- : Whether to always display minutes, or only if nonzero. Possible values are
seconds
- : The style of the formatted seconds.
- If
minutes
is"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.style
if it's not"digital"
, and"numeric"
otherwise.
- If
- : The style of the formatted seconds.
secondsDisplay
- : Whether to always display seconds, or only if nonzero. Possible values are
"always"
and"auto"
; the default is"auto"
ifseconds
is unspecified andoptions.style
is not"digital"
, and"always"
otherwise.
- : Whether to always display seconds, or only if nonzero. Possible values are
milliseconds
- : The style of the formatted milliseconds.
- If
seconds
is"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.style
if it's not"digital"
, and"numeric"
otherwise.
- If
- : The style of the formatted milliseconds.
millisecondsDisplay
- : Whether to always display milliseconds, or only if nonzero.
- If
seconds
is"numeric"
or"2-digit"
, the only possible value is"auto"
; the default is only"auto"
whenmilliseconds
is unspecified. - Otherwise, possible values are
"always"
and"auto"
; the default is"auto"
ifmilliseconds
is unspecified, and"always"
otherwise.
- If
- : Whether to always display milliseconds, or only if nonzero.
microseconds
- : The style of the formatted microseconds.
- If
milliseconds
is"numeric"
, the only possible value is"numeric"
; the default is"numeric"
. - Otherwise, possible values are
"long"
,"short"
,"narrow"
, and"numeric"
; the default isoptions.style
if it's not"digital"
, and"numeric"
otherwise.
- If
- : The style of the formatted microseconds.
microsecondsDisplay
- : Whether to always display microseconds, or only if nonzero.
- If
milliseconds
is"numeric"
, the only possible value is"auto"
; the default is only"auto"
whenmicroseconds
is unspecified. - Otherwise, possible values are
"always"
and"auto"
; the default is"auto"
ifmicroseconds
is unspecified, and"always"
otherwise.
- If
- : Whether to always display microseconds, or only if nonzero.
nanoseconds
- : The style of the formatted nanoseconds.
- If
microseconds
is"numeric"
, the only possible value is"numeric"
; the default is"numeric"
. - Otherwise, possible values are
"long"
,"short"
,"narrow"
, and"numeric"
; the default isoptions.style
if it's not"digital"
, and"numeric"
otherwise.
- If
- : The style of the formatted nanoseconds.
nanosecondsDisplay
- : Whether to always display nanoseconds, or only if nonzero.
- If
microseconds
is"numeric"
, the only possible value is"auto"
; the default is only"auto"
whennanoseconds
is unspecified. - Otherwise, possible values are
"always"
and"auto"
; the default is"auto"
ifnanoseconds
is unspecified, and"always"
otherwise.
- If
- : Whether to always display nanoseconds, or only if nonzero.
fractionalDigits
- : Number of how many fractional second digits to display in the output. Possible values are from
0
to9
; the default isundefined
(include as many fractional digits as necessary).
- : Number of how many fractional second digits to display in the output. Possible values are from
Exceptions
- RangeError
- : Thrown if
locales
oroptions
contain invalid values.
- : Thrown if
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
:0
whenoptions.fractionalDigits
isundefined
,options.fractionalDigits
otherwisemaximumFractionDigits
:9
whenoptions.fractionalDigits
isundefined
,options.fractionalDigits
otherwiseroundingMode
:"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,undefined
otherwiseunit
: 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"