Skip to content

Commit cbce13f

Browse files
committed
revision to Intl.DurationFormat.format to reflect recent normative changes related to fractionalDigits option
1 parent d546a54 commit cbce13f

File tree

1 file changed

+26
-26
lines changed
  • files/en-us/web/javascript/reference/global_objects/intl/durationformat/format

1 file changed

+26
-26
lines changed

files/en-us/web/javascript/reference/global_objects/intl/durationformat/format/index.md

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@ format(duration)
2020
### Parameters
2121

2222
- `duration`
23-
- : 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"`.
23+
- : 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`.
2424

2525
## Examples
2626

27-
### Using format with an object as parameter
27+
### Using format
28+
29+
The following example shows how to create a Duration formatter using the English language.
2830

2931
```js
3032
const duration = {
@@ -42,15 +44,7 @@ const duration = {
4244

4345
// Without options, style defaults to "short"
4446
new Intl.DurationFormat("en").format(duration);
45-
// "1 yr, 2 mths, 3 wks, 3 days, 4 hr, 5 min, 6 sec, 7 ms, 8 μs, 9 ns"
46-
47-
// With style set to "long"
48-
new Intl.DurationFormat("en", { style: "long" }).format(duration);
49-
// "1 year, 2 months, 3 weeks, 3 days, 4 hours, 5 minutes, 6 seconds, 7 milliseconds, 8 microseconds, 9 nanoseconds"
50-
51-
// With style set to "narrow"
52-
new Intl.DurationFormat("en", { style: "narrow" }).format(duration);
53-
// "1y 2mo 3w 3d 4h 5m 6s 7ms 8μs 9ns"
47+
// "1 yr, 2 mths, 3 wks, 3 days, 4 hr, 5 min, 6 sec, 7 ms, 8 μs, 9 ns"
5448
```
5549

5650
### Using format() with different locales and styles
@@ -64,42 +58,48 @@ const duration = {
6458

6559
// With style set to "long" and locale "fr-FR"
6660
new Intl.DurationFormat("fr-FR", { style: "long" }).format(duration);
67-
// "1 heure, 46 minutes et 40 secondes"
61+
// "1 heure, 46 minutes et 40 secondes"
6862

6963
// With style set to "short" and locale set to "en"
7064
new Intl.DurationFormat("en", { style: "short" }).format(duration);
71-
// "1 hr, 46 min and 40 sec"
65+
// "1 hr, 46 min and 40 sec"
7266

7367
// With style set to "short" and locale set to "pt"
7468
new Intl.DurationFormat("pt", { style: "narrow" }).format(duration);
75-
// "1h 46min 40s"
69+
// "1h 46min 40s"
7670

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

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

84-
Use the `format` getter function for formatting using with fractionalDigits options and setting milliseconds display to `narrow`.
80+
### Using the fractionalDigits option
8581

8682
```js
8783
const duration = {
84+
hours: 11,
85+
minutes: 30,
8886
seconds: 12,
8987
milliseconds: 345,
9088
microseconds: 600,
9189
};
9290

93-
// Example using fractionalDigits
94-
new Intl.DurationFormat("en", { fractionalDigits: 2 }).format(duration);
95-
// => 12 sec, 345 ms, 600 μs
91+
new Intl.DurationFormat("en", { style: "digital" }).format(duration);
92+
// "11:30:12.3456"
93+
94+
new Intl.DurationFormat("en", { style: "digital", fractionalDigits: 5 }).format(
95+
duration,
96+
);
97+
// "11:30:12.34560"
9698

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

105105
## Specifications

0 commit comments

Comments
 (0)