The format()
method of Intl.DurationFormat
instances formats a duration according to the locale and formatting options of this Intl.DurationFormat
object.
Parameters
-
duration
-
The duration object to be formatted. It should include some or all of the following properties: months
, weeks
, days
, hours
, minutes
, seconds
, milliseconds
, microseconds
, nanoseconds
.
Return value
A string representing the given duration
formatted according to the locale and formatting options of this Intl.DurationFormat
object.
The following example shows how to create a Duration formatter using the English language.
const duration = {
years: 1,
months: 2,
weeks: 3,
days: 3,
hours: 4,
minutes: 5,
seconds: 6,
milliseconds: 7,
microseconds: 8,
nanoseconds: 9,
};
new Intl.DurationFormat("en").format(duration);
new Intl.DurationFormat("en", { style: "long" }).format(duration);
new Intl.DurationFormat("en", { style: "narrow" }).format(duration);
const duration = {
hours: 1,
minutes: 46,
seconds: 40,
};
new Intl.DurationFormat("fr-FR", { style: "long" }).format(duration);
new Intl.DurationFormat("en", { style: "short" }).format(duration);
new Intl.DurationFormat("pt", { style: "narrow" }).format(duration);
new Intl.DurationFormat("en", { style: "digital" }).format(duration);
new Intl.DurationFormat("en", { style: "digital", hours: "long" }).format(
duration,
);
const duration = {
hours: 11,
minutes: 30,
seconds: 12,
milliseconds: 345,
microseconds: 600,
};
new Intl.DurationFormat("en", { style: "digital" }).format(duration);
new Intl.DurationFormat("en", { style: "digital", fractionalDigits: 5 }).format(
duration,
);
new Intl.DurationFormat("en", { style: "digital", fractionalDigits: 3 }).format(
duration,
);
Specifications
Browser compatibility
|
Desktop |
Mobile |
Server |
|
Chrome |
Edge |
Firefox |
Opera |
Safari |
Chrome Android |
Firefox for Android |
Opera Android |
Safari on IOS |
Samsung Internet |
WebView Android |
Deno |
Node.js |
format |
No |
No |
No |
No |
16.4 |
No |
No |
No |
16.4 |
No |
No |
No |
No |