-
Notifications
You must be signed in to change notification settings - Fork 22.9k
Add single-digit example to toLocaleString() #41410
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
Merged
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b86f994
add example
dipikabh 0490bc3
edit fixes
dipikabh e15b31e
Update files/en-us/web/javascript/reference/global_objects/date/toloc…
dipikabh 3c0722c
remove example, update code and prose
dipikabh 61565e1
fix period
dipikabh fe73a2b
remove 2-digit reference
dipikabh 5377688
remove 2-digit reference
dipikabh d3a6fa0
Apply suggestions from linter
dipikabh 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 |
|---|---|---|
|
|
@@ -45,7 +45,7 @@ In implementations that support the [`Intl.DateTimeFormat` API](/en-US/docs/Web/ | |
| In implementations without `Intl.DateTimeFormat` support, this parameter is ignored and the host's locale is usually used. | ||
|
|
||
| - `options` {{optional_inline}} | ||
| - : An object adjusting the output format. Corresponds to the [`options`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/DateTimeFormat#options) parameter of the `Intl.DateTimeFormat()` constructor. If `weekday`, `year`, `month`, `day`, `dayPeriod`, `hour`, `minute`, `second`, and `fractionalSecondDigits` are all undefined, then `year`, `month`, `day`, `hour`, `minute`, `second` will be set to `"numeric"`. | ||
| - : An object adjusting the output format. Corresponds to the [`options`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/DateTimeFormat#options) parameter of the `Intl.DateTimeFormat()` constructor. If `weekday`, `year`, `month`, `day`, `dayPeriod`, `hour`, `minute`, `second`, and `fractionalSecondDigits` are all undefined, then `year`, `month`, `day`, `hour`, `minute`, `second` will be set to `"numeric"`. To show leading zeros, use `"2-digit"` for the relevant field. | ||
dipikabh marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| In implementations without `Intl.DateTimeFormat` support, this parameter is ignored. | ||
|
|
||
|
|
@@ -64,13 +64,11 @@ In implementations with `Intl.DateTimeFormat`, this is equivalent to `new Intl.D | |
|
|
||
| ### Using toLocaleString() | ||
|
|
||
| Basic use of this method without specifying a `locale` returns a formatted string in the default locale and with default options. | ||
| Basic use of this method – without specifying `locale` or `options` – depends on the implementation and returns a string formatted based on the default locale and time zone, and with default options. | ||
|
|
||
| ```js | ||
| const date = new Date(Date.UTC(2012, 11, 12, 3, 0, 0)); | ||
|
|
||
| // toLocaleString() without arguments depends on the | ||
| // implementation, the default locale, and the default time zone | ||
|
Comment on lines
-72
to
-73
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. This comment seemed like a repeat of the prose before the code. |
||
| console.log(date.toLocaleString()); | ||
| // "12/11/2012, 7:00:00 PM" if run in en-US locale with time zone America/Los_Angeles | ||
| ``` | ||
|
|
@@ -94,36 +92,36 @@ function toLocaleStringSupportsLocales() { | |
| This example shows some of the variations in localized date and time formats. In order to get the format of the language used in the user interface of your application, make sure to specify that language (and possibly some fallback languages) using the `locales` argument: | ||
|
|
||
| ```js | ||
| const date = new Date(Date.UTC(2012, 11, 20, 3, 0, 0)); | ||
| const date = new Date(Date.UTC(2012, 1, 2, 3, 0, 0)); | ||
|
|
||
| // Formats below assume the local time zone of the locale; | ||
| // America/Los_Angeles for the US | ||
|
|
||
| // US English uses month-day-year order and 12-hour time with AM/PM | ||
| console.log(date.toLocaleString("en-US")); | ||
| // "12/19/2012, 7:00:00 PM" | ||
| // "2/1/2012, 7:00:00 PM" (UTC-8 is the previous day) | ||
|
|
||
| // British English uses day-month-year order and 24-hour time without AM/PM | ||
| console.log(date.toLocaleString("en-GB")); | ||
| // "20/12/2012 03:00:00" | ||
| // "02/02/2012, 03:00:00" (UTC+0 or UTC+1 depending on time of the year) | ||
|
|
||
| // Korean uses year-month-day order and 12-hour time with AM/PM | ||
| console.log(date.toLocaleString("ko-KR")); | ||
| // "2012. 12. 20. 오후 12:00:00" | ||
| // "2012. 2. 2. 오후 12:00:00" | ||
|
|
||
| // Arabic in most Arabic-speaking countries uses Eastern Arabic numerals | ||
| console.log(date.toLocaleString("ar-EG")); | ||
| // "٢٠/١٢/٢٠١٢ ٥:٠٠:٠٠ ص" | ||
| // "٢/٢/٢٠١٢ ٥:٠٠:٠٠ ص" | ||
|
|
||
| // For Japanese, applications may want to use the Japanese calendar, | ||
| // where 2012 was the year 24 of the Heisei era | ||
| console.log(date.toLocaleString("ja-JP-u-ca-japanese")); | ||
| // "24/12/20 12:00:00" | ||
| // "H24/2/2 12:00:00" | ||
|
|
||
| // When requesting a language that may not be supported, such as | ||
| // Balinese, include a fallback language (in this case, Indonesian) | ||
| console.log(date.toLocaleString(["ban", "id"])); | ||
| // "20/12/2012 11.00.00" | ||
| // "2/2/2012 11.00.00" | ||
| ``` | ||
|
|
||
| ### Using options | ||
|
|
@@ -141,19 +139,24 @@ const options = { | |
| day: "numeric", | ||
| }; | ||
| console.log(date.toLocaleString("de-DE", options)); | ||
| // "Donnerstag, 20. Dezember 2012" | ||
| // Example output: "Donnerstag, 20. Dezember 2012" | ||
| // The exact date may shift depending on your local time zone. | ||
|
|
||
| // An application may want to use UTC and make that visible | ||
| options.timeZone = "UTC"; | ||
| options.timeZoneName = "short"; | ||
| console.log(date.toLocaleString("en-US", options)); | ||
| // "Thursday, December 20, 2012, GMT" | ||
| // Example output: "Thursday, December 20, 2012 at UTC" | ||
|
|
||
| // Sometimes even the US needs 24-hour time | ||
| console.log(date.toLocaleString("en-US", { hour12: false })); | ||
| // "12/19/2012, 19:00:00" | ||
| // Example output: "12/19/2012, 19:00:00" | ||
| // The exact date and time may shift depending on your local time zone. | ||
| ``` | ||
|
|
||
| To force a fixed-width day or month, use [`2-digit`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/DateTimeFormat#2-digit) for the respective option. | ||
| The output may still follow the locale's style (for example, `en-GB` often adds leading zeros even with `numeric`). | ||
dipikabh marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
dipikabh marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ## Specifications | ||
|
|
||
| {{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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Again, I don't think this should be documented on this page because it opens an bottomless hole about what kind of options should be documented on this page. The existing sentence about "If
weekday,year,month,day,dayPeriod,hour,minute,second, andfractionalSecondDigitsare all undefined, thenyear,month,day,hour,minute,secondwill be set to"numeric"." is because this behavior is specific totoLocaleString. All behaviors shared byIntl.DateTimeFormatandtoLocaleStringshould be documented in the former.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, I've removed the mentions of
2-digitfrom this page.