├── .npmrc
├── .gitattributes
├── package.json
├── .gitignore
├── LICENSE
├── README.md
└── spec.emu
/.npmrc:
--------------------------------------------------------------------------------
1 | package-lock=false
2 |
--------------------------------------------------------------------------------
/.gitattributes:
--------------------------------------------------------------------------------
1 | index.html -diff merge=ours
2 | spec.js -diff merge=ours
3 | spec.css -diff merge=ours
4 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "private": true,
3 | "name": "ecma402-intl-DateTimeFormat-formatRange",
4 | "description": "Proposal to add formatRange and formatRangeToParts to DateTimeFormat.",
5 | "scripts": {
6 | "build": "ecmarkup spec.emu > index.html"
7 | },
8 | "homepage": "https://github.com/tc39/proposal-intl-DateTimeFormat-formatRange#readme",
9 | "repository": {
10 | "type": "git",
11 | "url": "git+https://github.com/tc39/proposal-intl-DateTimeFormat-formatRange.git"
12 | },
13 | "license": "MIT",
14 | "devDependencies": {
15 | "ecmarkup": "^3.11.5"
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Logs
2 | logs
3 | *.log
4 | npm-debug.log*
5 |
6 | # Runtime data
7 | pids
8 | *.pid
9 | *.seed
10 |
11 | # Directory for instrumented libs generated by jscoverage/JSCover
12 | lib-cov
13 |
14 | # Coverage directory used by tools like istanbul
15 | coverage
16 |
17 | # nyc test coverage
18 | .nyc_output
19 |
20 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
21 | .grunt
22 |
23 | # node-waf configuration
24 | .lock-wscript
25 |
26 | # Compiled binary addons (http://nodejs.org/api/addons.html)
27 | build/Release
28 |
29 | # Dependency directories
30 | node_modules
31 | jspm_packages
32 |
33 | # Optional npm cache directory
34 | .npm
35 |
36 | # Optional REPL history
37 | .node_repl_history
38 |
39 | # Only apps should have lockfiles
40 | yarn.lock
41 | package-lock.json
42 | npm-shrinkwrap.json
43 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2017 ECMA TC39 and contributors
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # `Intl.DateTimeFormat.prototype.formatRange`
2 |
3 | ## Overview
4 |
5 | ### Motivation
6 |
7 | It's common for websites to display date intervals or date ranges to show the
8 | span of an event, such as a hotel reservation, the billing period of a
9 | service, or other similar uses. In order to implement this, websites often
10 | use localization libraries, such as Google Closure, to format the date range,
11 | or they may simply resort to formatting both dates independently.
12 |
13 | If following the second alternative, web developers may encounter problems such
14 | as repeating fields that are common between the two dates, inappropriate order
15 | of the dates for the locale or using an incorrect delimiter for the locale.
16 |
17 | For example:
18 |
19 | ```javascript
20 | let date1 = new Date(Date.UTC(2007, 0, 10)); // "Jan 10, 2007"
21 | let date2 = new Date(Date.UTC(2007, 0, 20)); // "Jan 20, 2007"
22 |
23 | let fmt = new Intl.DateTimeFormat('en', {
24 | year: 'numeric',
25 | month: 'long',
26 | day: 'numeric'
27 | });
28 |
29 | // The second date's 'month' and 'year' calendar fields are redundant, only
30 | // 'day' provides new information.
31 | console.log(`${fmt.format(date1)} – ${fmt.format(date2)}`);
32 | // > 'January 10, 2007 – January 20, 2007'
33 | ```
34 |
35 | Formatting date ranges in a concise and locale-aware way requires a
36 | non-negligible amount of raw or compiled data: localized patterns are needed for
37 | all the possible combinations of displayed calendar fields (e.g. `month`, `day`,
38 | `hour`), their lengths (e.g. `2-digit`, `numeric`) and which is the largest
39 | different calendar field between the range's two dates.
40 |
41 | For example, formatting the following ranges using the same options (`{year:
42 | 'numeric', month: 'short', day: 'numeric'}`) requires two different patterns:
43 |
44 | Date range from **January 10, 2017** to **January 20, 2017**:
45 |
46 | * `'Jan 10 – 20, 2007'`
47 | * Largest different calendar field: `'day'`
48 |
49 | Date range from **January 10, 2017** to **February 20, 2017**:
50 |
51 | * `'Jan 10 – Feb 20, 2007'`
52 | * Largest different calendar field: `'month'`
53 |
54 | Bringing this functionality into the platform will improve performance of the
55 | web and developer productivity as they will be able to format date ranges
56 | correctly, flexibly and concisely without the extra locale data size and
57 | overhead.
58 |
59 | ### Status
60 |
61 | **Stage 4**
62 |
63 | * Initial Discussion: https://github.com/tc39/ecma402/issues/188
64 | * PR: https://github.com/tc39/ecma402/pull/532 (merged)
65 |
66 | Polyfill: [@formatjs/intl-datetimeformat](https://www.npmjs.com/package/@formatjs/intl-datetimeformat)
67 |
68 | ## Proposal
69 |
70 | Add `formatRange(date1, date2)` and `formatRangeToParts(date1, date2)` to
71 | `Intl.DateTimeFormat` to enable date range formatting.
72 |
73 | This proposal is based on the ICU Date Interval Formatter and on the Unicode
74 | CLDR TR-35 spec on date intervals.
75 |
76 | * http://icu-project.org/apiref/icu4j/com/ibm/icu/text/DateIntervalFormat.html
77 | * https://unicode.org/reports/tr35/tr35-dates.html#intervalFormats
78 |
79 | ### API
80 |
81 | #### `Intl.DateTimeFormat.prototype.formatRange(date1, date2)`
82 |
83 | This method receives two `Dates` and formats the date range in the most concise
84 | way based on the `locale` and `options` provided when instantiating
85 | `Intl.DateTimeFormat`.
86 |
87 | #### `Intl.DateTimeFormat.prototype.formatRangeToParts(date1, date2)`
88 |
89 | This method receives two `Dates` and returns an `Array` of objects containing
90 | the locale-specific tokens representing each part of the formatted date range.
91 |
92 | See:
93 | [`Intl.DateTimeFormat.prototype.formatToParts`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DateTimeFormat/formatToParts).
94 |
95 | ### Example Usage
96 |
97 | #### `Intl.DateTimeFormat.prototype.formatRange(date1, date2)`
98 |
99 | ```javascript
100 | let date1 = new Date(Date.UTC(2007, 0, 10, 10, 0, 0));
101 | let date2 = new Date(Date.UTC(2007, 0, 10, 11, 0, 0));
102 | let date3 = new Date(Date.UTC(2007, 0, 20, 10, 0, 0));
103 | // > 'Wed, 10 Jan 2007 10:00:00 GMT'
104 | // > 'Wed, 10 Jan 2007 11:00:00 GMT'
105 | // > 'Sat, 20 Jan 2007 10:00:00 GMT'
106 |
107 | let fmt1 = new Intl.DateTimeFormat("en", {
108 | year: '2-digit',
109 | month: 'numeric',
110 | day: 'numeric',
111 | hour: 'numeric',
112 | minute: 'numeric'
113 | });
114 | console.log(fmt1.format(date1));
115 | console.log(fmt1.formatRange(date1, date2));
116 | console.log(fmt1.formatRange(date1, date3));
117 | // > '1/10/07, 10:00 AM'
118 | // > '1/10/07, 10:00 – 11:00 AM'
119 | // > '1/10/07, 10:00 AM – 1/20/07, 10:00 AM'
120 |
121 | let fmt2 = new Intl.DateTimeFormat("en", {
122 | year: 'numeric',
123 | month: 'short',
124 | day: 'numeric'
125 | });
126 | console.log(fmt2.format(date1));
127 | console.log(fmt2.formatRange(date1, date2));
128 | console.log(fmt2.formatRange(date1, date3));
129 | // > 'Jan 10, 2007'
130 | // > 'Jan 10, 2007'
131 | // > 'Jan 10 – 20, 2007'
132 | ```
133 |
134 | #### `Intl.DateTimeFormat.prototype.formatRangeToParts(date1, date2)`
135 |
136 | ```javascript
137 | let date1 = new Date(Date.UTC(2007, 0, 10, 10, 0, 0));
138 | let date2 = new Date(Date.UTC(2007, 0, 10, 11, 0, 0));
139 | // > 'Wed, 10 Jan 2007 10:00:00 GMT'
140 | // > 'Wed, 10 Jan 2007 11:00:00 GMT'
141 |
142 | let fmt = new Intl.DateTimeFormat("en", {
143 | hour: 'numeric',
144 | minute: 'numeric'
145 | });
146 |
147 | console.log(fmt.formatRange(date1, date2));
148 | // > '10:00 – 11:00 AM'
149 |
150 | fmt.formatRangeToParts(date1, date2);
151 | // return value:
152 | // [
153 | // { type: 'hour', value: '10', source: "startRange" },
154 | // { type: 'literal', value: ':', source: "startRange" },
155 | // { type: 'minute', value: '00', source: "startRange" },
156 | // { type: 'literal', value: ' – ', source: "shared" },
157 | // { type: 'hour', value: '11', source: "endRange" },
158 | // { type: 'literal', value: ':', source: "endRange" },
159 | // { type: 'minute', value: '00', source: "endRange" },
160 | // { type: 'literal', value: ' ', source: "shared" },
161 | // { type: 'dayPeriod', value: 'AM', source: "shared" }
162 | // ]
163 | ```
164 |
165 | ## Alternatives
166 |
167 | ### `Intl.DateIntervalFormat`
168 |
169 | Instead of adding `.formatRange()` and `.formatRangeToParts()` to
170 | `Intl.DateTimeFormat`, a separate date interval formatter can be added. The API
171 | would be the following:
172 |
173 | * `new Intl.DateIntervalFormat(locale, options)`: `options` are the same
174 | options currently used in `Intl.DateTimeFormat`.
175 | * `Intl.DateIntervalFormat.prototype.format(date1, date2)`
176 | * `Intl.DateIntervalFormat.prototype.formatToParts(date1, date2)`
177 |
178 | Benefits:
179 |
180 | * Consistent with other I18n libraries such as ICU, which provide a separate
181 | date interval formatter.
182 |
183 | Drawbacks:
184 |
185 | * Support for ranges can be added to other formatters (e.g
186 | `Intl.NumberFormat`) by simply providing a `.formatRange()` method, avoiding
187 | the need to create an additional range formatter for every regular
188 | formatter.
189 | * The `options` used to instantiate a `Intl.DateIntervalFormat` are the same
190 | ones used for
191 | [`Intl.DateTimeFormat`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DateTimeFormat).
192 | Any options added to `Intl.DateTimeFormat` will need to be replicated in
193 | `Intl.DateIntervalFormat`.
194 |
195 | ## Prior art
196 |
197 | * ICU
198 | [com.ibm.icu.text.DateIntervalFormat](http://icu-project.org/apiref/icu4j/com/ibm/icu/text/DateIntervalFormat.html)
199 | * Apple Foundation
200 | [NSDateIntervalFormatter](https://developer.apple.com/documentation/foundation/nsdateintervalformatter)
201 | * Closure
202 | [goog.i18n.DateIntervalFormat](https://google.github.io/closure-library/api/goog.i18n.DateIntervalFormat.html)
203 |
--------------------------------------------------------------------------------
/spec.emu:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
22 | Several DateTimeFormat algorithms use values from the following table, which provides internal slots, property names and allowable values for the components of date and time formats:
23 |
24 |
25 |
26 | Components of date and time formats
27 |
97 | The abstract operation InitializeDateTimeFormat accepts the arguments _dateTimeFormat_ (which must be an object), _locales_, and _options_. It initializes _dateTimeFormat_ as a DateTimeFormat object. This abstract operation functions as follows:
98 |
103 |
104 |
105 | 1. Let _requestedLocales_ be ? CanonicalizeLocaleList(_locales_).
106 | 1. Let _options_ be ? ToDateTimeOptions(_options_, *"any"*, *"date"*).
107 | 1. Let _opt_ be a new Record.
108 | 1. Let _matcher_ be ? GetOption(_options_, *"localeMatcher"*, *"string"*, « *"lookup"*, *"best fit"* », *"best fit"*).
109 | 1. Set _opt_.[[localeMatcher]] to _matcher_.
110 | 1. Let _calendar_ be ? GetOption(_options_, *"calendar"*, *"string"*, *undefined*, *undefined*).
111 | 1. If _calendar_ is not *undefined*, then
112 | 1. If _calendar_ does not match the Unicode Locale Identifier `type` nonterminal, throw a *RangeError* exception.
113 | 1. Set _opt_.[[ca]] to _calendar_.
114 | 1. Let _numberingSystem_ be ? GetOption(_options_, *"numberingSystem"*, *"string"*, *undefined*, *undefined*).
115 | 1. If _numberingSystem_ is not *undefined*, then
116 | 1. If _numberingSystem_ does not match the Unicode Locale Identifier `type` nonterminal, throw a *RangeError* exception.
117 | 1. Set _opt_.[[nu]] to _numberingSystem_.
118 | 1. Let _hour12_ be ? GetOption(_options_, *"hour12"*, *"boolean"*, *undefined*, *undefined*).
119 | 1. Let _hourCycle_ be ? GetOption(_options_, *"hourCycle"*, *"string"*, « *"h11"*, *"h12"*, *"h23"*, *"h24"* », *undefined*).
120 | 1. If _hour12_ is not *undefined*, then
121 | 1. Let _hourCycle_ be *null*.
122 | 1. Set _opt_.[[hc]] to _hourCycle_.
123 | 1. Let _localeData_ be %DateTimeFormat%.[[LocaleData]].
124 | 1. Let _r_ be ResolveLocale(%DateTimeFormat%.[[AvailableLocales]], _requestedLocales_, _opt_, %DateTimeFormat%.[[RelevantExtensionKeys]], _localeData_).
125 | 1. Set _dateTimeFormat_.[[Locale]] to _r_.[[locale]].
126 | 1. Let _calendar_ be _r_.[[ca]].
127 | 1. Set _dateTimeFormat_.[[Calendar]] to _calendar_.
128 | 1. Set _dateTimeFormat_.[[HourCycle]] to _r_.[[hc]].
129 | 1. Set _dateTimeFormat_.[[NumberingSystem]] to _r_.[[nu]].
130 | 1. Let _dataLocale_ be _r_.[[dataLocale]].
131 | 1. Let _timeZone_ be ? Get(_options_, *"timeZone"*).
132 | 1. If _timeZone_ is *undefined*, then
133 | 1. Let _timeZone_ be DefaultTimeZone().
134 | 1. Else,
135 | 1. Let _timeZone_ be ? ToString(_timeZone_).
136 | 1. If the result of IsValidTimeZoneName(_timeZone_) is *false*, then
137 | 1. Throw a *RangeError* exception.
138 | 1. Let _timeZone_ be CanonicalizeTimeZoneName(_timeZone_).
139 | 1. Set _dateTimeFormat_.[[TimeZone]] to _timeZone_.
140 | 1. Let _opt_ be a new Record.
141 | 1. For each row of , except the header row, in table order, do
142 | 1. Let _prop_ be the name given in the Property column of the row.
143 | 1. If _prop_ is *"fractionalSecondDigits"*, then
144 | 1. Let _value_ be ? GetNumberOption(options, *"fractionalSecondDigits"*, 1, 3, *undefined*).
145 | 1. Else,
146 | 1. Let _value_ be ? GetOption(_options_, _prop_, *"string"*, « the strings given in the Values column of the row », *undefined*).
147 | 1. Set _opt_.[[<_prop_>]] to _value_.
148 | 1. Let _dataLocaleData_ be _localeData_.[[<_dataLocale_>]].
149 | 1. Let _matcher_ be ? GetOption(_options_, *"formatMatcher"*, *"string"*, « *"basic"*, *"best fit"* », *"best fit"*).
150 | 1. Let _dateStyle_ be ? GetOption(_options_, *"dateStyle"*, *"string"*, « *"full"*, *"long"*, *"medium"*, *"short"* », *undefined*).
151 | 1. Set _dateTimeFormat_.[[DateStyle]] to _dateStyle_.
152 | 1. Let _timeStyle_ be ? GetOption(_options_, *"timeStyle"*, *"string"*, « *"full"*, *"long"*, *"medium"*, *"short"* », *undefined*).
153 | 1. Set _dateTimeFormat_.[[TimeStyle]] to _timeStyle_.
154 | 1. If _dateStyle_ is not *undefined* or _timeStyle_ is not *undefined*, then
155 | 1. For each row in , except the header row, do
156 | 1. Let _prop_ be the name given in the Property column of the row.
157 | 1. Let _p_ be _opt_.[[<_prop_>]].
158 | 1. If _p_ is not *undefined*, then
159 | 1. Throw a *TypeError* exception.
160 | 1. Let _bestFormat_ be DateTimeStyleFormat(_dateStyle_, _timeStyle_, _dataLocaleData_).
161 | 1. Else,
162 | 1. Let _formats_ be _dataLocaleData_.[[formats]].[[<_calendar_>]].
163 | 1. If _matcher_ is *"basic"*, then
164 | 1. Let _bestFormat_ be BasicFormatMatcher(_opt_, _formats_).
165 | 1. Else,
166 | 1. Let _bestFormat_ be BestFitFormatMatcher(_opt_, _formats_).
167 | 1. For each row in , except the header row, in table order, do
168 | 1. Let _prop_ be the name given in the Property column of the row.
169 | 1. Let _p_ be _bestFormat_.[[<_prop_>]].
170 | 1. If _p_ not *undefined*, then
171 | 1. Set _dateTimeFormat_'s internal slot whose name is the Internal Slot column of the row to _p_.
172 | 1. If _dateTimeFormat_.[[Hour]] is *undefined*, then
173 | 1. Set _dateTimeFormat_.[[HourCycle]] to *undefined*.
174 | 1. Let _pattern_ be _bestFormat_.[[pattern]].
175 | 1. Let _rangePatterns_ be _bestFormat_.[[rangePatterns]].
176 | 1. Else,
177 | 1. Let _hcDefault_ be _dataLocaleData_.[[hourCycle]].
178 | 1. Let _hc_ be _dateTimeFormat_.[[HourCycle]].
179 | 1. If _hc_ is *null*, then
180 | 1. Set _hc_ to _hcDefault_.
181 | 1. If _hour12_ is not *undefined*, then
182 | 1. If _hour12_ is *true*, then
183 | 1. If _hcDefault_ is *"h11"* or *"h23"*, then
184 | 1. Set _hc_ to *"h11"*.
185 | 1. Else,
186 | 1. Set _hc_ to *"h12"*.
187 | 1. Else,
188 | 1. Assert: _hour12_ is *false*.
189 | 1. If _hcDefault_ is *"h11"* or *"h23"*, then
190 | 1. Set _hc_ to *"h23"*.
191 | 1. Else,
192 | 1. Set _hc_ to *"h24"*.
193 | 1. Set _dateTimeFormat_.[[HourCycle]] to _hc_.
194 | 1. If _dateTimeformat_.[[HourCycle]] is *"h11"* or *"h12"*, then
195 | 1. Let _pattern_ be _bestFormat_.[[pattern12]].
196 | 1. Let _rangePatterns_ be _bestFormat_.[[rangePatterns12]].
197 | 1. Else,
198 | 1. Let _pattern_ be _bestFormat_.[[pattern]].
199 | 1. Let _rangePatterns_ be _bestFormat_.[[rangePatterns]].
200 | 1. Set _dateTimeFormat_.[[Pattern]] to _pattern_.
201 | 1. Set _dateTimeFormat_.[[RangePatterns]] to _rangePatterns_.
202 | 1. Return _dateTimeFormat_.
203 |
204 |
205 |
206 |
207 |
210 | When the ToDateTimeOptions abstract operation is called with arguments _options_, _required_, and _defaults_, the following steps are taken:
211 |
212 |
213 |
214 | 1. If _options_ is *undefined*, let _options_ be *null*; otherwise let _options_ be ? ToObject(_options_).
215 | 1. Let _options_ be ObjectCreate(_options_).
216 | 1. Let _needDefaults_ be *true*.
217 | 1. If _required_ is *"date"* or *"any"*, then
218 | 1. For each of the property names *"weekday"*, *"year"*, *"month"*, *"day"*, do
219 | 1. Let _prop_ be the property name.
220 | 1. Let _value_ be ? Get(_options_, _prop_).
221 | 1. If _value_ is not *undefined*, let _needDefaults_ be *false*.
222 | 1. If _required_ is *"time"* or *"any"*, then
223 | 1. For each of the property names *"dayPeriod"*, *"hour"*, *"minute"*, *"second"*, *"fractionalSecondDigits"*, do
224 | 1. Let _prop_ be the property name.
225 | 1. Let _value_ be ? Get(_options_, _prop_).
226 | 1. If _value_ is not *undefined*, let _needDefaults_ be *false*.
227 | 1. Let _dateStyle_ be ? Get(_options_, *"dateStyle"*).
228 | 1. Let _timeStyle_ be ? Get(_options_, *"timeStyle"*).
229 | 1. If _dateStyle_ is not *undefined* or _timeStyle_ is not *undefined*, let _needDefaults_ be *false*.
230 | 1. If _required_ is *"date"* and _timeStyle_ is not *undefined*, then
231 | 1. Throw a *TypeError* exception.
232 | 1. If _required_ is *"time"* and _dateStyle_ is not *undefined*, then
233 | 1. Throw a *TypeError* exception.
234 | 1. If _needDefaults_ is *true* and _defaults_ is either *"date"* or *"all"*, then
235 | 1. For each of the property names *"year"*, *"month"*, *"day"*, do
236 | 1. Perform ? CreateDataPropertyOrThrow(_options_, _prop_, *"numeric"*).
237 | 1. If _needDefaults_ is *true* and _defaults_ is either *"time"* or *"all"*, then
238 | 1. For each of the property names *"hour"*, *"minute"*, *"second"*, do
239 | 1. Perform ? CreateDataPropertyOrThrow(_options_, _prop_, *"numeric"*).
240 | 1. Return _options_.
241 |
242 |
243 |
244 |
245 |
The DateTimeStyleFormat abstract operation accepts arguments _dateStyle_ and _timeStyle_, which are each either *undefined*, *"full"*, *"long"*, *"medium"*, or *"short"*, at least one of which is not *undefined*, and _dataLocaleData_, which is a record from %DateTimeFormat%.[[LocaleData]][_locale_] for some locale _locale_. It returns the appropriate format record for date time formatting based on the parameters.
247 |
248 | 1. If _timeStyle_ is not *undefined*, then
249 | 1. Assert: _timeStyle_ is one of *"full"*, *"long"*, *"medium"*, or *"short"*.
250 | 1. Let _timeFormat_ be _dataLocaleData_.[[TimeFormat]].[[<_timeStyle_>]].
251 | 1. If _dateStyle_ is not *undefined*, then
252 | 1. Assert: _dateStyle_ is one of *"full"*, *"long"*, *"medium"*, or *"short"*.
253 | 1. Let _dateFormat_ be _dataLocaleData_.[[DateFormat]].[[<_dateStyle_>]].
254 | 1. If _dateStyle_ is not *undefined* and _timeStyle_ is not *undefined*, then
255 | 1. Let _format_ be a new Record.
256 | 1. Add to _format_ all fields from _dateFormat_ except [[pattern]] and [[rangePatterns]].
257 | 1. Add to _format_ all fields from _timeFormat_ except [[pattern]], [[rangePatterns]], [[pattern12]] and [[rangePatterns12]], if present.
258 | 1. Let _connector_ be _dataLocaleData_.[[DateTimeFormat]].[[<_dateStyle_>]].
259 | 1. Let _pattern_ be the string _connector_ with the substring *"{0}"* replaced with _timeFormat_.[[pattern]] and the substring *"{1}"* replaced with _dateFormat_.[[pattern]].
260 | 1. Set _format_.[[pattern]] to _pattern_.
261 | 1. If _timeFormat_ has a [[pattern12]] field, then
262 | 1. Let _pattern12_ be the string _connector_ with the substring *"{0}"* replaced with _timeFormat_.[[pattern12]] and the substring *"{1}"* replaced with _dateFormat_.[[pattern]].
263 | 1. Set _format_.[[pattern12]] to _pattern12_.
264 | 1. Let _dateTimeRangeFormat_ be _dataLocaleData_.[[DateTimeRangeFormat]].[[<_dateStyle_>]].[[<_timeStyle_>]]
265 | 1. Add to _format_ all fields from _dateTimeRangeFormat_ including [[rangePatterns]] and [[rangePatterns12]], if present.
266 | 1. Return _format_.
267 | 1. If _timeStyle_ is not *undefined*, then
268 | 1. Return _timeFormat_.
269 | 1. Assert: _dateStyle_ is not *undefined*.
270 | 1. Return _dateFormat_.
271 |
272 |
273 |
274 |
275 |
BasicFormatMatcher ( _options_, _formats_ )
276 |
277 |
278 | When the BasicFormatMatcher abstract operation is called with two arguments _options_ and _formats_, the following steps are taken:
279 |
280 |
281 |
282 | 1. Let _removalPenalty_ be 120.
283 | 1. Let _additionPenalty_ be 20.
284 | 1. Let _longLessPenalty_ be 8.
285 | 1. Let _longMorePenalty_ be 6.
286 | 1. Let _shortLessPenalty_ be 6.
287 | 1. Let _shortMorePenalty_ be 3.
288 | 1. Let _bestScore_ be -*Infinity*.
289 | 1. Let _bestFormat_ be *undefined*.
290 | 1. Assert: Type(_formats_) is List.
291 | 1. For each element _format_ of _formats_ in List order, do
292 | 1. Let _score_ be 0.
293 | 1. For each property name _property_ shown in , do
294 | 1. Let _optionsProp_ be _options_.[[<_property_>]].
295 | 1. Let _formatProp_ be _format_.[[<_property_>]].
296 | 1. If _optionsProp_ is *undefined* and _formatProp_ is not *undefined*, decrease _score_ by _additionPenalty_.
297 | 1. Else if _optionsProp_ is not *undefined* and _formatProp_ is *undefined*, decrease _score_ by _removalPenalty_.
298 | 1. Else if _optionsProp_ ≠ _formatProp_, then
299 | 1. If _property_ is *"fractionalSecondDigits"*, then
300 | 1. Let _values_ be « *1*, *2*, *3* ».
301 | 1. Else,
302 | 1. Let _values_ be « *"2-digit"*, *"numeric"*, *"narrow"*, *"short"*, *"long"* ».
303 | 1. Let _optionsPropIndex_ be the index of _optionsProp_ within _values_.
304 | 1. Let _formatPropIndex_ be the index of _formatProp_ within _values_.
305 | 1. Let _delta_ be max(min(_formatPropIndex_ - _optionsPropIndex_, 2), -2).
306 | 1. If _delta_ = 2, decrease _score_ by _longMorePenalty_.
307 | 1. Else if _delta_ = 1, decrease _score_ by _shortMorePenalty_.
308 | 1. Else if _delta_ = -1, decrease _score_ by _shortLessPenalty_.
309 | 1. Else if _delta_ = -2, decrease _score_ by _longLessPenalty_.
310 | 1. If _score_ > _bestScore_, then
311 | 1. Let _bestScore_ be _score_.
312 | 1. Let _bestFormat_ be _format_.
313 | 1. Return _bestFormat_.
314 |
315 |
316 |
317 |
318 |
BestFitFormatMatcher ( _options_, _formats_ )
319 |
320 |
321 | When the BestFitFormatMatcher abstract operation is called with two arguments _options_ and _formats_, it performs implementation dependent steps, which should return a set of component representations that a typical user of the selected locale would perceive as at least as good as the one returned by BasicFormatMatcher.
322 |
323 |
324 |
325 |
326 |
DateTime Format Functions
327 |
328 |
A DateTime format function is an anonymous built-in function that has a [[DateTimeFormat]] internal slot.
329 |
When a DateTime format function _F_ is called with optional argument _date_, the following steps are taken:
330 |
331 |
332 | 1. Let _dtf_ be _F_.[[DateTimeFormat]].
333 | 1. Assert: Type(_dtf_) is Object and _dtf_ has an [[InitializedDateTimeFormat]] internal slot.
334 | 1. If _date_ is not provided or is *undefined*, then
335 | 1. Let _x_ be Call(%Date.now%, *undefined*).
336 | 1. Else,
337 | 1. Let _x_ be ? ToNumber(_date_).
338 | 1. Return ? FormatDateTime(_dtf_, _x_).
339 |
340 |
341 |
342 | The *"length"* property of a DateTime format function is 1.
343 |
350 | The FormatDateTimePattern abstract operation is called with arguments _dateTimeFormat_ (which must be an object initialized as a DateTimeFormat), _patternParts_ (which is a list of Records as returned by PartitionPattern), _x_ (which must be a Number value), and _rangeFormatOptions_ (which is a range pattern Record as used in [[rangePattern]] or *undefined*), interprets _x_ as a time value as specified in ES2021, , and creates the corresponding parts according _pattern_ and to the effective locale and the formatting options of _dateTimeFormat_ and _rangeFormatOptions_. The following steps are taken:
351 |
352 |
353 |
354 | 1. Let _x_ be TimeClip(_x_).
355 | 1. If _x_ is *NaN*, throw a *RangeError* exception.
356 | 1. Let _locale_ be _dateTimeFormat_.[[Locale]].
357 | 1. Let _nfOptions_ be ObjectCreate(*null*).
358 | 1. Perform ! CreateDataPropertyOrThrow(_nfOptions_, *"useGrouping"*, *false*).
359 | 1. Let _nf_ be ? Construct(%NumberFormat%, « _locale_, _nfOptions_ »).
360 | 1. Let _nf2Options_ be ObjectCreate(*null*).
361 | 1. Perform ! CreateDataPropertyOrThrow(_nf2Options_, *"minimumIntegerDigits"*, 2).
362 | 1. Perform ! CreateDataPropertyOrThrow(_nf2Options_, *"useGrouping"*, *false*).
363 | 1. Let _nf2_ be ? Construct(%NumberFormat%, « _locale_, _nf2Options_ »).
364 | 1. Let _fractionalSecondDigits_ be _dateTimeFormat_.[[FractionalSecondDigits]].
365 | 1. If _fractionalSecondDigits_ is not *undefined*, then
366 | 1. Let _nf3Options_ be ObjectCreate(*null*).
367 | 1. Perform ! CreateDataPropertyOrThrow(_nf3Options_, *"minimumIntegerDigits"*, _fractionalSecondDigits_).
368 | 1. Perform ! CreateDataPropertyOrThrow(_nf3Options_, *"useGrouping"*, *false*).
369 | 1. Let _nf3_ be ? Construct(%NumberFormat%, « _locale_, _nf3Options_ »).
370 | 1. Let _tm_ be ToLocalTime(_x_, _dateTimeFormat_.[[Calendar]], _dateTimeFormat_.[[TimeZone]]).
371 | 1. Let _result_ be a new empty List.
372 | 1. Let _patternParts_ be PartitionPattern(_dateTimeFormat_.[[Pattern]]).
373 | 1. For each element _patternPart_ of _patternParts_, in List order, do
374 | 1. Let _p_ be _patternPart_.[[Type]].
375 | 1. If _p_ is *"literal"*, then
376 | 1. Append a new Record { [[Type]]: *"literal"*, [[Value]]: _patternPart_.[[Value]] } as the last element of the list _result_.
377 | 1. Else if _p_ is equal to *"fractionalSecondDigits"*, then
378 | 1. Let _v_ be _tm_.[[Millisecond]].
379 | 1. Let _v_ be floor(_v_ × 10( _fractionalSecondDigits_ - 3 )).
380 | 1. Let _fv_ be FormatNumeric(_nf3_, _v_).
381 | 1. Append a new Record { [[Type]]: *"fractionalSecond"*, [[Value]]: _fv_ } as the last element of _result_.
382 | 1. Else if _p_ matches a Property column of the row in , then
383 | 1. If _rangeFormatOptions_ is not *undefined*, let _f_ be the value of _rangeFormatOptions_'s field whose name matches _p_.
384 | 1. Else, let _f_ be the value of _dateTimeFormat_'s internal slot whose name is the Internal Slot column of the matching row.
385 | 1. Let _v_ be the value of _tm_'s field whose name is the Internal Slot column of the matching row.
386 | 1. If _p_ is *"year"* and _v_ ≤ 0, let _v_ be 1 - _v_.
387 | 1. If _p_ is *"month"*, increase _v_ by 1.
388 | 1. If _p_ is *"hour"* and _dateTimeFormat_.[[HourCycle]] is *"h11"* or *"h12"*, then
389 | 1. Let _v_ be _v_ modulo 12.
390 | 1. If _v_ is 0 and _dateTimeFormat_.[[HourCycle]] is *"h12"*, let _v_ be 12.
391 | 1. If _p_ is *"hour"* and _dateTimeFormat_.[[HourCycle]] is *"h24"*, then
392 | 1. If _v_ is 0, let _v_ be 24.
393 | 1. If _f_ is *"numeric"*, then
394 | 1. Let _fv_ be FormatNumeric(_nf_, _v_).
395 | 1. Else if _f_ is *"2-digit"*, then
396 | 1. Let _fv_ be FormatNumeric(_nf2_, _v_).
397 | 1. If the *"length"* property of _fv_ is greater than 2, let _fv_ be the substring of _fv_ containing the last two characters.
398 | 1. Else if _f_ is *"narrow"*, *"short"*, or *"long"*, then let _fv_ be a String value representing _v_ in the form given by _f_; the String value depends upon the implementation and the effective locale and calendar of _dateTimeFormat_. If _p_ is *"month"* and _rangeFormatOptions_ is *undefined*, then the String value may also depend on whether _dateTimeFormat_.[[Day]] is *undefined*. If _p_ is *"month"* and _rangeFormatOptions_ is not *undefined*, then the String value may also depend on whether _rangeFormatOptions_.[[day]] is *undefined*. If _p_ is *"timeZoneName"*, then the String value may also depend on the value of the [[InDST]] field of _tm_. If _p_ is *"era"* and _rangeFormatOptions_ is *undefined*, then the String value may also depend on whether _dateTimeFormat_.[[Era]] is *undefined*. If _p_ is *"era"* and _rangeFormatOptions_ is not *undefined*, then the String value may also depend on whether _rangeFormatOptions_.[[era]] is *undefined*. If the implementation does not have a localized representation of _f_, then use _f_ itself.
399 | 1. Append a new Record { [[Type]]: _p_, [[Value]]: _fv_ } as the last element of the list _result_.
400 | 1. Else if _p_ is equal to *"ampm"*, then
401 | 1. Let _v_ be _tm_.[[Hour]].
402 | 1. If _v_ is greater than 11, then
403 | 1. Let _fv_ be an implementation and locale dependent String value representing *"post meridiem"*.
404 | 1. Else,
405 | 1. Let _fv_ be an implementation and locale dependent String value representing *"ante meridiem"*.
406 | 1. Append a new Record { [[Type]]: *"dayPeriod"*, [[Value]]: _fv_ } as the last element of the list _result_.
407 | 1. Else if _p_ is equal to *"relatedYear"*, then
408 | 1. Let _v_ be _tm_.[[RelatedYear]].
409 | 1. Let _fv_ be FormatNumeric(_nf_, _v_).
410 | 1. Append a new Record { [[Type]]: *"relatedYear"*, [[Value]]: _fv_ } as the last element of the list _result_.
411 | 1. Else if _p_ is equal to *"yearName"*, then
412 | 1. Let _v_ be _tm_.[[YearName]].
413 | 1. Append a new Record { [[Type]]: *"yearName"*, [[Value]]: _v_ } as the last element of the list _result_.
414 | 1. Else,
415 | 1. Let _unknown_ be an implementation-, locale-, and numbering system-dependent String based on _x_ and _p_.
416 | 1. Append a new Record { [[Type]]: *"unknown"*, [[Value]]: _unknown_ } as the last element of _result_.
417 | 1. Return _result_.
418 |
419 |
420 |
421 | It is recommended that implementations use the locale and calendar dependent strings provided by the Common Locale Data Repository (available at http://cldr.unicode.org), and use CLDR *"abbreviated"* strings for DateTimeFormat *"short"* strings, and CLDR *"wide"* strings for DateTimeFormat *"long"* strings.
422 |
423 |
424 |
425 | It is recommended that implementations use the time zone information of the IANA Time Zone Database.
426 |
427 |
428 |
429 |
430 |
431 |
434 | The PartitionDateTimePattern abstract operation is called with arguments _dateTimeFormat_ (which must be an object initialized as a DateTimeFormat) and _x_ (which must be a Number value), interprets _x_ as a time value as specified in ES2021, , and creates the corresponding parts according to the effective locale and the formatting options of _dateTimeFormat_. The following steps are taken:
435 |
436 |
437 |
438 | 1. Let _patternParts_ be PartitionPattern(_dateTimeFormat_.[[Pattern]]).
439 | 1. Let _result_ be ? FormatDateTimePattern(_dateTimeFormat_, _patternParts_, _x_, *undefined*).
440 | 1. Return _result_.
441 |
442 |
443 |
444 |
445 |
446 |
FormatDateTime ( _dateTimeFormat_, _x_ )
447 |
448 |
449 | The FormatDateTime abstract operation is called with arguments _dateTimeFormat_ (which must be an object initialized as a DateTimeFormat) and _x_ (which must be a Number value), and performs the following steps:
450 |
451 |
452 |
453 | 1. Let _parts_ be ? PartitionDateTimePattern(_dateTimeFormat_, _x_).
454 | 1. Let _result_ be the empty String.
455 | 1. For each Record _part_ in _parts_, do
456 | 1. Set _result_ to the string-concatenation of _result_ and _part_.[[Value]].
457 | 1. Return _result_.
458 |
459 |
460 |
461 |
462 |
FormatDateTimeToParts ( _dateTimeFormat_, _x_ )
463 |
464 |
465 | The FormatDateTimeToParts abstract operation is called with arguments _dateTimeFormat_ (which must be an object initialized as a DateTimeFormat) and _x_ (which must be a Number value), and performs the following steps:
466 |
467 |
468 |
469 | 1. Let _parts_ be ? PartitionDateTimePattern(_dateTimeFormat_, _x_).
470 | 1. Let _result_ be ArrayCreate(0).
471 | 1. Let _n_ be 0.
472 | 1. For each Record _part_ in _parts_, do
473 | 1. Let _O_ be ObjectCreate(%Object.prototype%).
474 | 1. Perform ! CreateDataPropertyOrThrow(_O_, *"type"*, _part_.[[Type]]).
475 | 1. Perform ! CreateDataPropertyOrThrow(_O_, *"value"*, _part_.[[Value]]).
476 | 1. Perform ! CreateDataProperty(_result_, ! ToString(_n_), _O_).
477 | 1. Increment _n_ by 1.
478 | 1. Return _result_.
479 |
480 |
481 |
482 |
483 |
484 |
485 |
488 | The PartitionDateTimeRangePattern abstract operation is called with arguments _dateTimeFormat_ (which must be an object initialized as a DateTimeFormat), _x_ (which must be a Number value) and _y_ (which must be a Number value), interprets _x_ and _y_ as time values as specified in ES2021, , and creates the corresponding parts according to the effective locale and the formatting options of _dateTimeFormat_. The following steps are taken:
489 |
490 |
491 |
492 | 1. Let _x_ be TimeClip(_x_).
493 | 1. If _x_ is *NaN*, throw a *RangeError* exception.
494 | 1. Let _y_ be TimeClip(_y_).
495 | 1. If _y_ is *NaN*, throw a *RangeError* exception.
496 | 1. If _x_ is greater than _y_, throw a *RangeError* exception.
497 | 1. Let _tm1_ be ToLocalTime(_x_, _dateTimeFormat_.[[Calendar]], _dateTimeFormat_.[[TimeZone]]).
498 | 1. Let _tm2_ be ToLocalTime(_y_, _dateTimeFormat_.[[Calendar]], _dateTimeFormat_.[[TimeZone]]).
499 | 1. Let _rangePatterns_ be _dateTimeFormat_.[[RangePatterns]].
500 | 1. Let _rangePattern_ be *undefined*.
501 | 1. Let _dateFieldsPracticallyEqual_ be *true*.
502 | 1. Let _patternContainsLargerDateField_ be *false*.
503 | 1. While _dateFieldsPracticallyEqual_ is *true* and _patternContainsLargerDateField_ is *false*, repeat for each row of in order, except the header row
504 | 1. Let _fieldName_ be the name given in the Range Pattern Field column of the row.
505 | 1. If _fieldName_ is equal to [[AmPm]], then
506 | 1. Let _rp_ be _rangePatterns_.[[AmPm]].
507 | 1. If _rangePattern_ is not *undefined* and _rp_ is *undefined*, then
508 | 1. Set _patternContainsLargerDateField_ to *true*.
509 | 1. Else,
510 | 1. Let _v1_ be _tm1_.[[Hour]].
511 | 1. Let _v2_ be _tm2_.[[Hour]].
512 | 1. If _v1_ is greater than 11 and _v2_ less or equal than 11, or _v1_ is less or equal than 11 and _v2_ is greater than 11, then
513 | 1. Set _dateFieldsPracticallyEqual_ to *false*.
514 | 1. Let _rangePattern_ be _rp_.
515 | 1. If _fieldName_ is equal to [[FractionalSecondDigits]], then
516 | 1. Let _rp_ be _rangePatterns_.[[FractionalSecondDigits]].
517 | 1. If _rangePattern_ is not *undefined* and _rp_ is *undefined*, then
518 | 1. Set _patternContainsLargerDateField_ to *true*.
519 | 1. Else,
520 | 1. Let _fractionalSecondDigits_ be _dateTimeFormat_.[[FractionalSecondDigits]].
521 | 1. If _fractionalSecondDigits_ is *undefined*, then
522 | 1. Set _fractionalSecondDigits_ to 3.
523 | 1. Let _v1_ be _tm1_.[[Millisecond]].
524 | 1. Let _v2_ be _tm2_.[[Millisecond]].
525 | 1. Let _v1_ be floor(_v1_ × 10( _fractionalSecondDigits_ - 3 )).
526 | 1. Let _v2_ be floor(_v2_ × 10( _fractionalSecondDigits_ - 3 )).
527 | 1. If _v1_ is not equal to _v2_, then
528 | 1. Set _dateFieldsPracticallyEqual_ to *false*.
529 | 1. Let _rangePattern_ be _rp_.
530 | 1. Else,
531 | 1. Let _rp_ be _rangePatterns_.[[<_fieldName_>]].
532 | 1. If _rangePattern_ is not *undefined* and _rp_ is *undefined*, then
533 | 1. Set _patternContainsLargerDateField_ to *true*.
534 | 1. Else,
535 | 1. Let _v1_ be _tm1_.[[<_fieldName_>]].
536 | 1. Let _v2_ be _tm2_.[[<_fieldName_>]].
537 | 1. If _v1_ is not equal to _v2_, then
538 | 1. Set _dateFieldsPracticallyEqual_ to *false*.
539 | 1. Let _rangePattern_ be _rp_.
540 | 1. If _dateFieldsPracticallyEqual_ is *true*, then
541 | 1. Let _pattern_ be _dateTimeFormat_.[[Pattern]].
542 | 1. Let _patternParts_ be PartitionPattern(_pattern_).
543 | 1. Let _result_ be ? FormatDateTimePattern(_dateTimeFormat_, _patternParts_, _x_, *undefined*).
544 | 1. For each _r_ in _result_ do
545 | 1. Set _r_.[[Source]] to *"shared"*.
546 | 1. Return _result_.
547 | 1. Let _result_ be a new empty List.
548 | 1. If _rangePattern_ is *undefined*, then
549 | 1. Let _rangePattern_ be _rangePatterns_.[[Default]].
550 | 1. For each _rangePatternPart_ in _rangePattern_.[[PatternParts]], do
551 | 1. Let _pattern_ be _rangePatternPart_.[[Pattern]].
552 | 1. Let _source_ be _rangePatternPart_.[[Source]]
553 | 1. If _source_ is *"startRange"* or *"shared"*, then
554 | 1. Let _z_ be _x_.
555 | 1. Else,
556 | 1. Let _z_ be _y_.
557 | 1. Let _patternParts_ be PartitionPattern(_pattern_).
558 | 1. Let _partResult_ be ? FormatDateTimePattern(_dateTimeFormat_, _patternParts_, _z_, _rangePattern_).
559 | 1. For each _r_ in _partResult_, do
560 | 1. Set _r_.[[Source]] to _source_.
561 | 1. Add all elements in _partResult_ to _result_ in order.
562 | 1. Return _result_.
563 |
564 |
565 |
566 |
567 |
568 |
569 |
570 |
FormatDateTimeRange( _dateTimeFormat_, _x_, _y_ )
571 |
572 |
573 | The FormatDateTimeRange abstract operation is called with arguments _dateTimeFormat_ (which must be an object initialized as a DateTimeFormat), _x_ (which must be a Number value) and _y_ (which must be a Number value), and performs the following steps:
574 |
575 |
576 |
577 | 1. Let _parts_ be ? PartitionDateTimeRangePattern(_dateTimeFormat_, _x_, _y_).
578 | 1. Let _result_ be the empty String.
579 | 1. For each Record _part_ in _parts_, do
580 | 1. Set _result_ to a String value produced by concatenating _result_ and _part_.[[Value]].
581 | 1. Return _result_.
582 |
583 |
584 |
585 |
586 |
587 |
588 |
589 |
592 | The FormatDateTimeRangeToParts abstract operation is called with arguments _dateTimeFormat_ (which must be an object initialized as a DateTimeFormat), _x_ (which must be a Number value) and _y_ (which must be a Number value), and performs the following steps:
593 |
594 |
595 |
596 | 1. Let _parts_ be ? PartitionDateTimeRangePattern(_dateTimeFormat_, _x_, _y_).
597 | 1. Let _result_ be ArrayCreate(0).
598 | 1. Let _n_ be 0.
599 | 1. For each Record _part_ in _parts_, do
600 | 1. Let _O_ be ObjectCreate(%ObjectPrototype%).
601 | 1. Perform ! CreateDataPropertyOrThrow(_O_, *"type"*, _part_.[[Type]]).
602 | 1. Perform ! CreateDataPropertyOrThrow(_O_, *"value"*, _part_.[[Value]]).
603 | 1. Perform ! CreateDataPropertyOrThrow(_O_, *"source"*, _part_.[[Source]]).
604 | 1. Perform ! CreateDataProperty(_result_, ! ToString(_n_), _O_).
605 | 1. Increment _n_ by 1.
606 | 1. Return _result_.
607 |
608 |
609 |
610 |
611 |
612 |
613 |
ToLocalTime ( _t_, _calendar_, _timeZone_ )
614 |
615 |
616 | When the ToLocalTime abstract operation is called with arguments _t_, _calendar_, and _timeZone_, the following steps are taken:
617 |
618 |
619 |
620 | 1. Assert: Type(_t_) is Number.
621 | 1. If _calendar_ is *"gregory"*, then
622 | 1. Let _timeZoneOffset_ be the value calculated according to LocalTZA(_t_, *true*) where the local time zone is replaced with timezone _timeZone_.
623 | 1. Let _tz_ be the time value _t_ + _timeZoneOffset_.
624 | 1. Return a record with fields calculated from _tz_ according to .
625 | 1. Else,
626 | 1. Return a record with the fields of Column 1 of calculated from _t_ for the given _calendar_ and _timeZone_. The calculations should use best available information about the specified _calendar_ and _timeZone_, including current and historical information about time zone offsets from UTC and daylight saving time rules.
627 |
628 |
629 |
630 | Record returned by ToLocalTime
631 |
632 |
633 |
634 |
Field
635 |
Value Calculation for Gregorian Calendar
636 |
637 |
638 |
639 |
[[Weekday]]
640 |
`WeekDay(tz)` specified in ES2021's Week Day
641 |
642 |
643 |
[[Era]]
644 |
Let `year` be `YearFromTime(tz)` specified in ES2021's Year Number. If `year` is less than 0, return 'BC', else, return 'AD'.
645 |
646 |
647 |
[[Year]]
648 |
`YearFromTime(tz)` specified in ES2021's Year Number
649 |
650 |
651 |
[[RelatedYear]]
652 |
*undefined*
653 |
654 |
655 |
[[YearName]]
656 |
*undefined*
657 |
658 |
659 |
[[Month]]
660 |
`MonthFromTime(tz)` specified in ES2021's Month Number
661 |
662 |
663 |
[[Day]]
664 |
`DateFromTime(tz)` specified in ES2021's Date Number
665 |
666 |
667 |
[[Hour]]
668 |
`HourFromTime(tz)` specified in ES2021's Hours, Minutes, Second, and Milliseconds
669 |
670 |
671 |
[[Minute]]
672 |
`MinFromTime(tz)` specified in ES2021's Hours, Minutes, Second, and Milliseconds
673 |
674 |
675 |
[[Second]]
676 |
`SecFromTime(tz)` specified in ES2021's Hours, Minutes, Second, and Milliseconds
677 |
678 |
679 |
[[Millisecond]]
680 |
`msFromTime(tz)` specified in ES2021's Hours, Minutes, Second, and Milliseconds
681 |
682 |
683 |
[[InDST]]
684 |
Calculate *true* or *false* using the best available information about the specified _calendar_ and _timeZone_, including current and historical information about time zone offsets from UTC and daylight saving time rules.
685 |
686 |
687 |
688 |
689 |
690 | It is recommended that implementations use the time zone information of the IANA Time Zone Database.
691 |
692 |
693 |
694 |
695 |
UnwrapDateTimeFormat ( _dtf_ )
696 |
697 | The UnwrapDateTimeFormat abstract operation gets the underlying DateTimeFormat operation
698 | for various methods which implement ECMA-402 v1 semantics for supporting initializing
699 | existing Intl objects.
700 |
701 |
702 | 1. Assert: Type(_dtf_) is Object.
703 |
704 |
705 |
706 |
707 | 2. If _dtf_ does not have an [[InitializedDateTimeFormat]] internal slot and ? InstanceofOperator(_dtf_, %DateTimeFormat%) is *true*, then
708 | 1. Let _dtf_ be ? Get(_dtf_, %Intl%.[[FallbackSymbol]]).
709 |
710 |
711 |
712 |
713 | 3. Perform ? RequireInternalSlot(_dtf_, [[InitializedDateTimeFormat]]).
714 | 1. Return _dtf_.
715 |
716 | See for the motivation of the normative optional text.
717 |
718 |
719 |
720 |
721 |
The Intl.DateTimeFormat Constructor
722 |
723 |
724 | The Intl.DateTimeFormat constructor is the %DateTimeFormat% intrinsic object and a standard built-in property of the Intl object. Behaviour common to all service constructor properties of the Intl object is specified in .
725 |
778 | When the `supportedLocalesOf` method is called with arguments _locales_ and _options_, the following steps are taken:
779 |
780 |
781 |
782 | 1. Let _availableLocales_ be %DateTimeFormat%.[[AvailableLocales]].
783 | 1. Let _requestedLocales_ be ? CanonicalizeLocaleList(_locales_).
784 | 1. Return ? SupportedLocales(_availableLocales_, _requestedLocales_, _options_).
785 |
786 |
787 |
788 | The value of the *"length"* property of the *supportedLocalesOf* method is 1.
789 |
790 |
791 |
792 |
793 |
Internal slots
794 |
795 |
796 | The value of the [[AvailableLocales]] internal slot is implementation defined within the constraints described in .
797 |
798 |
799 |
800 | The value of the [[RelevantExtensionKeys]] internal slot is « *"ca"*, *"hc"*, *"nu"* ».
801 |
802 |
803 |
804 | Unicode Technical Standard 35 describes four locale extension keys that are relevant to date and time formatting: *"ca"* for calendar, *"hc"* for hour cycle, *"nu"* for numbering system (of formatted numbers), and *"tz"* for time zone. DateTimeFormat, however, requires that the time zone is specified through the *"timeZone"* property in the options objects.
805 |
806 |
807 |
808 | The value of the [[LocaleData]] internal slot is implementation defined within the constraints described in and the following additional constraints, for all locale values _locale_:
809 |
810 |
811 |
812 |
813 | [[LocaleData]].[[<_locale_>]].[[nu]] must be a List that does not include the values *"native"*, *"traditio"*, or *"finance"*.
814 |
815 |
816 | [[LocaleData]].[[<_locale_>]].[[hc]] must be « *null*, *"h11"*, *"h12"*, *"h23"*, *"h24"* ».
817 |
818 |
819 | [[LocaleData]].[[<_locale_>]].[[hourCycle]] must be a String value equal to *"h11"*, *"h12"*, *"h23"*, or *"h24"*.
820 |
821 |
822 | [[LocaleData]].[[<_locale_>]] must have a [[formats]] field. This formats field must have a [[<_calendar_>]] field for all calendar values _calendar_. The value of this field must be a list of records, each of which has a subset of the fields shown in , where each field must have one of the values specified for the field in . Multiple records in a list may use the same subset of the fields as long as they have different values for the fields. The following subsets must be available for each locale:
823 |
837 | Each of the records must also have the following fields:
838 |
839 |
A [[pattern]] field, whose value is a String value that contains for each of the date and time format component fields of the record a substring starting with *"{"*, followed by the name of the field, followed by *"}"*.
840 |
If the record has an hour field, it must also have a [[pattern12]] field, whose value is a String value that, in addition to the substrings of the [[pattern]] field, contains a substring *"{ampm}"*.
841 |
If the record has a [[year]] field, the [[pattern]] and [[pattern12]] values may contain the substrings *"{yearName}"* and *"{relatedYear}"*
842 |
843 | A [[rangePatterns]] field with a Record value
844 |
845 |
The [[rangePatterns]] record may have any of the fields in , where each field represents a range pattern and it's value is a Record.
846 |
847 |
The name of the field indicates the largest calendar element that must be different between the start and end dates in order to use this range pattern. For example, if the field name is Month, it contains the range pattern that should be used to format a date range where the Era and Year values are the same, but Month value is different.
848 |
The record will contain the following fields:
849 |
850 |
A subset of the fields shown in the Property column of , where each field must have one of the values specified for that field in the Values column of . Only the fields required to format a date with any of the PatternParts will be added.
851 |
A [[PatternParts]] field whose value is a list of Records each representing a part of the range pattern. Each record contains a [[Pattern]] field and a [[Source]] field. The [[Pattern]] field's value is a String of the same format as the regular date pattern String. The [[Source]] field must contain one of the following values *"shared"*, *"startRange"* or *"endRange"* which indicates which of the range's dates should be formatted using the value of the Pattern field.
852 |
853 |
854 |
855 |
The [[rangePatterns]] record must have a [[Default]] field which contains the default range pattern used when the specific range pattern is not available. It's value is a list of records with the same structure as the other fields in the [[rangePatterns]] record.
856 |
857 |
858 |
If the record has an hour field, it must also have a [[rangePatterns12]] field. It's value is similar to the Record in [[rangePatterns]], but it uses a String similar to [[pattern12]] for each part of the range pattern.
859 |
If the record has a [[year]] field, the [[rangePatterns]] and [[rangePatterns12]] may contain range patterns where the [[Pattern]] values may contain the substrings *"{yearName}"* and *"{relatedYear}"*
860 |
861 |
862 |
863 | [[LocaleData]][<_locale_>] must have a [[styles]] field. The styles field must have a [[<_calendar_>]] field for all calendar values _calendar_. The field must contain [[DateFormat]], [[TimeFormat]], [[DateTimeFormat]] and [[DateTimeRangeFormat]] fields, the value of these fields are Records, where each of which has [[full]], [[long]], [[medium]] and [[short]] fields. For [[DateFormat]] and [[TimeFormat]], the value of these fields must be a record, which has a subset of the fields shown in Table 1, where each field must have one of the values specified for the field in Table 1. Each of the records must also have the following fields:
864 |
865 |
A [[pattern]] field, whose value is a String value that contains for each of the date and time format component fields of the record a substring starting with *"{"*, followed by the name of the field, followed by *"}"*.
866 |
If the record has an hour field, it must also have a [[pattern12]] field, whose value is a String value that, in addition to the substrings of the pattern field, contains a substring *"{ampm}"*.
867 |
A [[rangePatterns]] field that contains a record similar to the one described in the [[formats]] field.
868 |
If the record has an hour field, it must also have a [[rangePatterns12]] field. It's value is similar to the record in [[rangePatterns]] but it uses a string similar to [[pattern12]] for each range pattern.
869 |
870 | For [[DateTimeFormat]], the field value must be a string pattern which contains the strings *"{0}"* and *"{1}"*. For [[DateTimeRangeFormat]] the value of these fields must be a nested record which also has a [[full]], [[long]], [[medium]] and [[short]] fields. The [[full]], [[long]], [[medium]] and [[short]] fields in the enclosing record refer to the date style of the range pattern, while the fields in the nested record refers to the time style of the range pattern. The value of these fields in the nested record is a record with a [[rangePatterns]] field and a [[rangePatterns12]] which are similar to the [[rangePatterns]] and [rangePatterns12]] fields in [[DateFormat]] and [[TimeFormat]].
871 |
872 |
873 |
874 |
875 | For example, an implementation might include the following record as part of its English locale data: {[[hour]]: *"numeric"*, [[minute]]: *"2-digit"*, [[second]]: *"2-digit"*, [[pattern]]: *"{hour}:{minute}:{second}"*, [[pattern12]]: *"{hour}:{minute}:{second} {ampm}"*}.
876 |
877 |
878 |
958 |
959 |
960 |
961 |
962 | It is recommended that implementations use the locale data provided by the Common Locale Data Repository (available at http://cldr.unicode.org).
963 |
964 |
965 |
966 | Range pattern fields
967 |
968 |
Properties of the Intl.DateTimeFormat Prototype Object
1020 |
1021 |
1022 | The Intl.DateTimeFormat prototype object is itself an ordinary object. %DateTimeFormat.prototype% is not an Intl.DateTimeFormat instance and does not have an [[InitializedDateTimeFormat]] internal slot or any of the other internal slots of Intl.DateTimeFormat instance objects.
1023 |
1024 |
1025 |
1026 |
Intl.DateTimeFormat.prototype.constructor
1027 |
1028 |
1029 | The initial value of `Intl.DateTimeFormat.prototype.constructor` is the intrinsic object %DateTimeFormat%.
1030 |
1031 |
1032 |
1033 |
1034 |
Intl.DateTimeFormat.prototype [ @@toStringTag ]
1035 |
1036 |
1037 | The initial value of the @@toStringTag property is the String value *"Intl.DateTimeFormat"*.
1038 |
1039 |
1040 |
1041 | This property has the attributes { [[Writable]]: *false*, [[Enumerable]]: *false*, [[Configurable]]: *true* }.
1042 |
1043 |
1044 |
1045 |
1046 |
get Intl.DateTimeFormat.prototype.format
1047 |
1048 |
1049 | Intl.DateTimeFormat.prototype.format is an accessor property whose set accessor function is *undefined*. Its get accessor function performs the following steps:
1050 |
1051 |
1052 |
1053 | 1. Let _dtf_ be the *this* value.
1054 | 1. If Type(_dtf_) is not Object, throw a *TypeError* exception.
1055 | 1. Let _dtf_ be ? UnwrapDateTimeFormat(_dtf_).
1056 | 1. If _dtf_.[[BoundFormat]] is *undefined*, then
1057 | 1. Let _F_ be a new built-in function object as defined in DateTime Format Functions ().
1058 | 1. Set _F_.[[DateTimeFormat]] to _dtf_.
1059 | 1. Set _dtf_.[[BoundFormat]] to _F_.
1060 | 1. Return _dtf_.[[BoundFormat]].
1061 |
1062 |
1063 |
1064 | The returned function is bound to _dtf_ so that it can be passed directly to `Array.prototype.map` or other functions.
1065 | This is considered a historical artefact, as part of a convention which is no longer followed for new features, but is preserved to maintain compatibility with existing programs.
1066 |
1067 |
1068 |
1069 |
1070 |
1111 | When the `formatRangeToParts` method is called with an arguments _startDate_ and _endDate_, the following steps are taken:
1112 |
1113 |
1114 |
1115 | 1. Let _dtf_ be *this* value.
1116 | 1. Perform ? RequireInternalSlot(_dtf_, [[InitializedDateTimeFormat]]).
1117 | 1. If _startDate_ is *undefined* or _endDate_ is *undefined*, throw a *TypeError* exception.
1118 | 1. Let _x_ be ? ToNumber(_startDate_).
1119 | 1. Let _y_ be ? ToNumber(_endDate_).
1120 | 1. Return ? FormatDateTimeRangeToParts(_dtf_, _x_, _y_).
1121 |
1122 |
1123 |
1124 |
1125 |
1126 |
Intl.DateTimeFormat.prototype.resolvedOptions ( )
1127 |
1128 |
1129 | This function provides access to the locale and formatting options computed during initialization of the object.
1130 |
1131 |
1132 |
1133 | 1. Let _dtf_ be the *this* value.
1134 | 1. If Type(_dtf_) is not Object, throw a *TypeError* exception.
1135 | 1. Let _dtf_ be ? UnwrapDateTimeFormat(_dtf_).
1136 | 1. Let _options_ be ! ObjectCreate(%Object.prototype%).
1137 | 1. For each row of , except the header row, in table order, do
1138 | 1. Let _p_ be the Property value of the current row.
1139 | 1. If _p_ is *"hour12"*, then
1140 | 1. Let _hc_ be _dtf_.[[HourCycle]].
1141 | 1. If _hc_ is *"h11"* or *"h12"*, let _v_ be *true*.
1142 | 1. Else if, _hc_ is *"h23"* or *"h24"*, let _v_ be *false*.
1143 | 1. Else, let _v_ be *undefined*.
1144 | 1. Else,
1145 | 1. Let _v_ be the value of _dtf_'s internal slot whose name is the Internal Slot value of the current row.
1146 | 1. If the Internal Slot value of the current row is an Internal Slot value in , then
1147 | 1. If _dtf_.[[DateStyle]] is not *undefined* or _dtf_.[[TimeStyle]] is not *undefined*, then
1148 | 1. Let _v_ be *undefined*.
1149 | 1. If _v_ is not *undefined*, then
1150 | 1. Perform ! CreateDataPropertyOrThrow(_options_, _p_, _v_).
1151 | 1. Return _options_.
1152 |
1153 |
1154 |
1155 | Resolved Options of DateTimeFormat Instances
1156 |
1157 |
1158 |
1159 |
Internal Slot
1160 |
Property
1161 |
1162 |
1163 |
1164 |
[[Locale]]
1165 |
*"locale"*
1166 |
1167 |
1168 |
[[Calendar]]
1169 |
*"calendar"*
1170 |
1171 |
1172 |
[[NumberingSystem]]
1173 |
*"numberingSystem"*
1174 |
1175 |
1176 |
[[TimeZone]]
1177 |
*"timeZone"*
1178 |
1179 |
1180 |
[[HourCycle]]
1181 |
*"hourCycle"*
1182 |
1183 |
1184 |
1185 |
*"hour12"*
1186 |
1187 |
1188 |
[[Weekday]]
1189 |
*"weekday"*
1190 |
1191 |
1192 |
[[Era]]
1193 |
*"era"*
1194 |
1195 |
1196 |
[[Year]]
1197 |
*"year"*
1198 |
1199 |
1200 |
[[Month]]
1201 |
*"month"*
1202 |
1203 |
1204 |
[[Day]]
1205 |
*"day"*
1206 |
1207 |
1208 |
[[DayPeriod]]
1209 |
*"dayPeriod"*
1210 |
1211 |
1212 |
[[Hour]]
1213 |
*"hour"*
1214 |
1215 |
1216 |
[[Minute]]
1217 |
*"minute"*
1218 |
1219 |
1220 |
[[Second]]
1221 |
*"second"*
1222 |
1223 |
1224 |
[[FractionalSecondDigits]]
1225 |
*"fractionalSecondDigits"*
1226 |
1227 |
1228 |
[[TimeZoneName]]
1229 |
*"timeZoneName"*
1230 |
1231 |
1232 |
[[DateStyle]]
1233 |
*"dateStyle"*
1234 |
1235 |
1236 |
[[TimeStyle]]
1237 |
*"timeStyle"*
1238 |
1239 |
1240 |
1241 |
1242 |
1243 | For web compatibility reasons, if the property *"hourCycle"* is set, the *"hour12"* property should be set to *true* when *"hourCycle"* is *"h11"* or *"h12"*, or to *false* when *"hourCycle"* is *"h23"* or *"h24"*.
1244 |
1245 |
1246 |
1247 | In this version of the ECMAScript 2021 Internationalization API, the *"timeZone"* property will be the name of the default time zone if no *"timeZone"* property was provided in the options object provided to the Intl.DateTimeFormat constructor. The first edition left the *"timeZone"* property *undefined* in this case.
1248 |
1249 |
1250 |
1251 | For compatibility with versions prior to the fifth edition, the *"hour12"* property is set in addition to the *"hourCycle"* property.
1252 |
1253 |
1254 |
1255 |
1256 |
1257 |
Properties of Intl.DateTimeFormat Instances
1258 |
1259 |
1260 | Intl.DateTimeFormat instances are ordinary objects that inherit properties from %DateTimeFormat.prototype%.
1261 |
1262 |
1263 |
1264 | Intl.DateTimeFormat instances have an [[InitializedDateTimeFormat]] internal slot.
1265 |
1266 |
1267 |
1268 | Intl.DateTimeFormat instances also have several internal slots that are computed by the constructor:
1269 |
1270 |
1271 |
1272 |
[[Locale]] is a String value with the language tag of the locale whose localization is used for formatting.
1273 |
[[Calendar]] is a String value with the *"type"* given in Unicode Technical Standard 35 for the calendar used for formatting.
1274 |
[[NumberingSystem]] is a String value with the *"type"* given in Unicode Technical Standard 35 for the numbering system used for formatting.
1275 |
[[TimeZone]] is a String value with the IANA time zone name of the time zone used for formatting.
1276 |
[[Weekday]], [[Era]], [[Year]], [[Month]], [[Day]], [[DayPeriod]], [[Hour]], [[Minute]], [[Second]], [[TimeZoneName]] are each either *undefined*, indicating that the component is not used for formatting, or one of the String values given in , indicating how the component should be presented in the formatted output.
1277 |
[[FractionalSecondDigits]] is either *undefined* or a positive, non-negative integer Number value indicating the fraction digits to be used for fractional seconds. Numbers will be rounded or padded with trailing zeroes if necessary.
1278 |
[[HourCycle]] is a String value indicating whether the 12-hour format (*"h11"*, *"h12"*) or the 24-hour format (*"h23"*, *"h24"*) should be used. *"h11"* and *"h23"* start with hour 0 and go up to 11 and 23 respectively. *"h12"* and *"h24"* start with hour 1 and go up to 12 and 24. [[HourCycle]] is only used when [[Hour]] is not *undefined*.
1279 |
[[DateStyle]], [[TimeStyle]] are each either *undefined*, or a String value with values *"full"*, *"long"*, *"medium"*, or *"short"*.
1280 |
[[Pattern]] is a String value as described in .
1281 |
[[RangePatterns]] is a Record as described in .
1282 |
1283 |
1284 |
1285 | Finally, Intl.DateTimeFormat instances have a [[BoundFormat]] internal slot that caches the function returned by the format accessor ().
1286 |