UNB/ CS/ David Bremner/ teaching/ cs2613/ books/ mdn/ Reference/ Global Objects/ Intl/ DurationFormat/ Intl.DurationFormat() constructor

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 with new. Attempting to call it without new throws a TypeError.

Parameters

Exceptions

Description

For each time segment, an Intl.NumberFormat object is constructed under the hood. It uses the following options (see Intl.NumberFormat() for details):

When milliseconds, microseconds, or nanoseconds uses the "numeric" style, the following options are also used:

When the time segment uses the "2-digit" style, the following options are also used:

When the time segment uses the "long", "short", or "narrow" style, the following options are also used:

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

Browser compatibility

See also