Skip to content
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ format(duration)
### 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"`.
- : 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`.

## Examples

### Using format with an object as parameter
### Using format
Comment thread
Josh-Cena marked this conversation as resolved.
Outdated

The following example shows how to create a Duration formatter using the English language.

```js
const duration = {
Expand All @@ -42,18 +44,10 @@ const duration = {

// Without options, style defaults to "short"
new Intl.DurationFormat("en").format(duration);
// "1 yr, 2 mths, 3 wks, 3 days, 4 hr, 5 min, 6 sec, 7 ms, 8 μs, 9 ns"

// With style set to "long"
new Intl.DurationFormat("en", { style: "long" }).format(duration);
// "1 year, 2 months, 3 weeks, 3 days, 4 hours, 5 minutes, 6 seconds, 7 milliseconds, 8 microseconds, 9 nanoseconds"

// With style set to "narrow"
new Intl.DurationFormat("en", { style: "narrow" }).format(duration);
// "1y 2mo 3w 3d 4h 5m 6s 7ms 8μs 9ns"
Comment on lines -47 to -53
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious why these are removed?

// "1 yr, 2 mths, 3 wks, 3 days, 4 hr, 5 min, 6 sec, 7 ms, 8 μs, 9 ns"
```

### Using format() with different locales and styles
### Using format with different locales and styles
Comment thread
Josh-Cena marked this conversation as resolved.
Outdated

```js
const duration = {
Expand All @@ -64,42 +58,48 @@ const duration = {

// With style set to "long" and locale "fr-FR"
new Intl.DurationFormat("fr-FR", { style: "long" }).format(duration);
// "1 heure, 46 minutes et 40 secondes"
// "1 heure, 46 minutes et 40 secondes"
Comment thread
Josh-Cena marked this conversation as resolved.
Outdated

// With style set to "short" and locale set to "en"
new Intl.DurationFormat("en", { style: "short" }).format(duration);
// "1 hr, 46 min and 40 sec"
// "1 hr, 46 min and 40 sec"
Comment thread
Josh-Cena marked this conversation as resolved.
Outdated

// With style set to "short" and locale set to "pt"
new Intl.DurationFormat("pt", { style: "narrow" }).format(duration);
// "1h 46min 40s"
// "1h 46min 40s"
Comment thread
Josh-Cena marked this conversation as resolved.
Outdated

// With style set to "digital" and locale set to "en"
new Intl.DurationFormat("en", { style: "digital" }).format(duration);
// "1:46:40"
```

### Using format with fractionalDigits option
// With style set to "digital", locale set to "en", and hours set to "long"
new Intl.DurationFormat("en", { style: "digital" }).format(duration);
// "1 hour, 46:40"
```

Use the `format` getter function for formatting using with fractionalDigits options and setting milliseconds display to `narrow`.
### Using format with the fractionalDigits option
Comment thread
Josh-Cena marked this conversation as resolved.
Outdated

```js
const duration = {
hours: 11,
minutes: 30,
seconds: 12,
milliseconds: 345,
microseconds: 600,
};

// Example using fractionalDigits
new Intl.DurationFormat("en", { fractionalDigits: 2 }).format(duration);
// => 12 sec, 345 ms, 600 μs
new Intl.DurationFormat("en", { style: "digital" }).format(duration);
// "11:30:12.3456"

new Intl.DurationFormat("en", { style: "digital", fractionalDigits: 5 }).format(
duration,
);
// "11:30:12.34560"

// Example using fractionalDigits and milliseconds set to `long`
new Intl.DurationFormat("en", {
milliseconds: "long",
fractionalDigits: 2,
}).format(duration);
// => 12 sec, 345 milliseconds, 600 μs
new Intl.DurationFormat("en", { style: "digital", fractionalDigits: 3 }).format(
duration,
);
// "11:30:12.346"
```

## Specifications
Expand Down