-
Notifications
You must be signed in to change notification settings - Fork 23.2k
Improve Intl.DurationFormat.format examples #28708
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Josh-Cena
merged 7 commits into
mdn:main
from
ben-allen:update-intl-durationformat-format-method
Aug 28, 2023
Merged
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
cbce13f
revision to Intl.DurationFormat.format to reflect recent normative ch…
ben-allen 7352b1b
regularize headings
ben-allen 5cb3908
Apply suggestions from code review
Josh-Cena 44b2ad0
restored two removed examples (style: "long" and style: "narrow")
ben-allen bf7dfc6
Fixes
Josh-Cena 81c75ff
Update files/en-us/web/javascript/reference/global_objects/intl/durat…
ben-allen a5f5435
Update index.md
Josh-Cena File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 = { | ||
|
|
@@ -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
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
Josh-Cena marked this conversation as resolved.
Outdated
|
||
|
|
||
| ```js | ||
| const duration = { | ||
|
|
@@ -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" | ||
|
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" | ||
|
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" | ||
|
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 | ||
|
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 | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.