An object {hours, minutes} written out as a phrase per locale, down to the digital style: how does it read in Indonesian? Checked first: not every browser has…
Why it matters
Durations are everywhere: video length, reading time, cooking time, a timer’s rest. Building them yourself (h + ' hours ' + m + ' minutes') breaks the grammar of other languages and drifts. DurationFormat gives one API, right per locale, with four lengths.
The key idea
A duration object becomes a local phrase, field by field
Common mistakes
DurationFormat throws in a browser that does not support it yet It is the newest of the Intl family; not every browser has it yet. Check typeof Intl.DurationFormat === 'function' and keep a fallback.
DurationFormat for "posted 2 hours ago" gives an odd result RelativeTimeFormat writes a point in time ("2 hours ago"); DurationFormat writes a length of time ("2 hours"). Different jobs, do not swap them.
DurationFormat.format(5400) errors or is wrong DurationFormat needs an object { hours, minutes, seconds }, not a total of seconds. Split it into its fields first, as whole numbers.
Try it in this playground
Pass the number 5400 DurationFormat throws a TypeError: it needs an object of fields, not a count of seconds.
Write { minutes: 90 } "90 minutes": as long as 1 hour 30 minutes on the ruler, but the field is written as it is.
Try style 'digital' Like a clock: 1:05:03 in en-US, 1.05.03 in id-ID.
Switch the locale One object, each language’s unit words and separators.