Skip to content
Merged
Changes from 3 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()

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

```js
const duration = {
Expand All @@ -42,15 +44,7 @@ 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
Expand All @@ -77,29 +71,35 @@ new Intl.DurationFormat("pt", { style: "narrow" }).format(duration);
// 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

```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