globalize-rpk 1.7.1 → 1.7.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CONTRIBUTING.md +5 -0
- package/README.md +818 -0
- package/{globalize → dist/globalize}/currency.js +3 -3
- package/{globalize → dist/globalize}/date.js +3 -3
- package/{globalize → dist/globalize}/message.js +2 -2
- package/{globalize → dist/globalize}/number.js +3 -3
- package/{globalize → dist/globalize}/plural.js +3 -3
- package/{globalize → dist/globalize}/relative-time.js +3 -3
- package/{globalize → dist/globalize}/unit.js +0 -0
- package/dist/globalize-runtime/currency.js +183 -0
- package/dist/globalize-runtime/date.js +1657 -0
- package/dist/globalize-runtime/message.js +120 -0
- package/dist/globalize-runtime/number.js +919 -0
- package/dist/globalize-runtime/plural.js +90 -0
- package/dist/globalize-runtime/relative-time.js +120 -0
- package/dist/globalize-runtime/unit.js +132 -0
- package/{globalize → dist}/globalize-runtime.js +0 -0
- package/{globalize → dist}/globalize.js +2 -2
- package/{node-main.js → dist/node-main.js} +0 -0
- package/doc/api/core/constructor.md +28 -0
- package/doc/api/core/load.md +96 -0
- package/doc/api/core/locale.md +43 -0
- package/doc/api/currency/currency-formatter.md +196 -0
- package/doc/api/currency/currency-to-parts-formatter.md +117 -0
- package/doc/api/date/date-formatter.md +203 -0
- package/doc/api/date/date-parser.md +60 -0
- package/doc/api/date/date-to-parts-formatter.md +176 -0
- package/doc/api/date/load-iana-time-zone.md +29 -0
- package/doc/api/message/load-messages.md +105 -0
- package/doc/api/message/message-formatter.md +208 -0
- package/doc/api/number/number-formatter.md +202 -0
- package/doc/api/number/number-parser.md +130 -0
- package/doc/api/number/number-to-parts-formatter.md +140 -0
- package/doc/api/plural/plural-generator.md +84 -0
- package/doc/api/relative-time/relative-time-formatter.md +60 -0
- package/doc/api/unit/unit-formatter.md +72 -0
- package/doc/blog-post/2017-07-xx-1.3.0-announcement.md +177 -0
- package/doc/cldr.md +114 -0
- package/doc/error/e-default-locale-not-defined.md +9 -0
- package/doc/error/e-invalid-cldr.md +14 -0
- package/doc/error/e-invalid-par-type.md +12 -0
- package/doc/error/e-invalid-par-value.md +11 -0
- package/doc/error/e-missing-cldr.md +11 -0
- package/doc/error/e-missing-parameter.md +10 -0
- package/doc/error/e-missing-plural-module.md +9 -0
- package/doc/error/e-par-missing-key.md +11 -0
- package/doc/error/e-par-out-of-range.md +13 -0
- package/doc/error/e-unsupported.md +10 -0
- package/doc/migrating-from-0.x.md +64 -0
- package/examples/amd-bower/.bowerrc +7 -0
- package/examples/amd-bower/README.md +65 -0
- package/examples/amd-bower/bower.json +13 -0
- package/examples/amd-bower/index.html +46 -0
- package/examples/amd-bower/main.js +141 -0
- package/examples/amd-bower/messages/en.json +12 -0
- package/examples/amd-bower/package.json +14 -0
- package/examples/app-npm-webpack/README.md +74 -0
- package/examples/app-npm-webpack/app/index.js +89 -0
- package/examples/app-npm-webpack/index-template.html +71 -0
- package/examples/app-npm-webpack/messages/ar.json +25 -0
- package/examples/app-npm-webpack/messages/de.json +21 -0
- package/examples/app-npm-webpack/messages/en.json +21 -0
- package/examples/app-npm-webpack/messages/es.json +21 -0
- package/examples/app-npm-webpack/messages/pt.json +21 -0
- package/examples/app-npm-webpack/messages/ru.json +23 -0
- package/examples/app-npm-webpack/messages/zh.json +20 -0
- package/examples/app-npm-webpack/package.json +17 -0
- package/examples/app-npm-webpack/webpack-config.js +63 -0
- package/examples/globalize-compiler/README.md +45 -0
- package/examples/globalize-compiler/app.js +58 -0
- package/examples/globalize-compiler/development.html +121 -0
- package/examples/globalize-compiler/messages.json +12 -0
- package/examples/globalize-compiler/package.json +15 -0
- package/examples/globalize-compiler/production.html +75 -0
- package/examples/node-npm/README.md +57 -0
- package/examples/node-npm/main.js +65 -0
- package/examples/node-npm/messages/en.json +12 -0
- package/examples/node-npm/package.json +10 -0
- package/examples/plain-javascript/README.md +81 -0
- package/examples/plain-javascript/index.html +445 -0
- package/package.json +27 -4
@@ -0,0 +1,196 @@
|
|
1
|
+
## .currencyFormatter( currency [, options] ) ➜ function( value )
|
2
|
+
|
3
|
+
Return a function that formats a `currency` according to the given `options` or locale's defaults.
|
4
|
+
|
5
|
+
The returned function is invoked with one argument: the Number `value` to be formatted.
|
6
|
+
|
7
|
+
### Parameters
|
8
|
+
|
9
|
+
#### currency
|
10
|
+
|
11
|
+
3-letter currency code as defined by ISO 4217, eg. `"USD"`.
|
12
|
+
|
13
|
+
#### options.style
|
14
|
+
|
15
|
+
Optional. String `"symbol"` (default), `"accounting"`, `"code"` or `"name"`. See [`.numberFormatter( [options] )`](../number/number-formatter.md) for more options.
|
16
|
+
|
17
|
+
#### value
|
18
|
+
|
19
|
+
Number to be formatted, eg. `9.99`.
|
20
|
+
|
21
|
+
### Example
|
22
|
+
|
23
|
+
#### Static Formatter
|
24
|
+
|
25
|
+
Prior to using any currency methods, you must load `cldr/main/{locale}/currencies.json`, `cldr/supplemental/currencyData.json`, and the CLDR content required by the number module. If using plural messages, you also must load the CLDR content required by the plural module. Read [CLDR content][] if you need more information.
|
26
|
+
|
27
|
+
[CLDR content]: ../../../README.md#2-cldr-content
|
28
|
+
|
29
|
+
#### Using the default options
|
30
|
+
|
31
|
+
You can use the static method `Globalize.currencyFormatter()`, which uses the default locale.
|
32
|
+
|
33
|
+
```javascript
|
34
|
+
var formatter;
|
35
|
+
|
36
|
+
Globalize.locale( "en" );
|
37
|
+
formatter = Globalize.currencyFormatter( "USD" );
|
38
|
+
|
39
|
+
formatter( 9.99 );
|
40
|
+
// > "$9.99"
|
41
|
+
```
|
42
|
+
|
43
|
+
#### Instance Formatter
|
44
|
+
|
45
|
+
You can use the instance method `.currencyFormatter()`, which uses the instance locale.
|
46
|
+
|
47
|
+
```javascript
|
48
|
+
var deFormatter = Globalize( "de" ).currencyFormatter( "EUR" ),
|
49
|
+
zhFormatter = Globalize( "zh" ).currencyFormatter( "EUR" );
|
50
|
+
|
51
|
+
deFormatter( 9.99 );
|
52
|
+
// > "9,99 €"
|
53
|
+
|
54
|
+
zhFormatter( 9.99 );
|
55
|
+
// > "€ 9.99"
|
56
|
+
|
57
|
+
```
|
58
|
+
|
59
|
+
For comparison, follow the formatting output of different symbols in different locales.
|
60
|
+
|
61
|
+
| 3-letter currency code | en (English) | de (German) | zh (Chinese) |
|
62
|
+
| ---------------------------------- | ------------ | ----------- | ------------ |
|
63
|
+
| `.currencyFormatter( "USD" )( 1 )` | `$1.00` | `1,00 $` | `US$ 1.00` |
|
64
|
+
| `.currencyFormatter( "EUR" )( 1 )` | `€1.00` | `1,00 €` | `€ 1.00` |
|
65
|
+
| `.currencyFormatter( "CNY" )( 1 )` | `CN¥1.00` | `1,00 CN¥` | `¥ 1.00` |
|
66
|
+
| `.currencyFormatter( "JPY" )( 1 )` | `¥1` | `1 ¥` | `JP¥ 1` |
|
67
|
+
| `.currencyFormatter( "GBP" )( 1 )` | `£1.00` | `1,00 £` | `£ 1.00` |
|
68
|
+
| `.currencyFormatter( "BRL" )( 1 )` | `R$1.00` | `1,00 R$` | `R$ 1.00` |
|
69
|
+
|
70
|
+
#### Using alternative `options.symbolForm`
|
71
|
+
|
72
|
+
Using the narrow symbol form, the same symbols may be used for multiple currencies. Thus the symbol may be ambiguous, and should only be used where the context is clear.
|
73
|
+
|
74
|
+
```js
|
75
|
+
Globalize( "en" ).currencyFormatter( "HKD" )( 1 );
|
76
|
+
// > "HK$1.00"
|
77
|
+
|
78
|
+
Globalize( "en" ).currencyFormatter( "HKD", { symbolForm: "narrow" } )( 1 );
|
79
|
+
// > "$1.00"
|
80
|
+
```
|
81
|
+
|
82
|
+
#### Configuring style
|
83
|
+
|
84
|
+
For the accounting variation of the symbol format, use `style: "accounting"`.
|
85
|
+
|
86
|
+
```javascript
|
87
|
+
var formatter = Globalize( "en" ).currencyFormatter( "USD", {
|
88
|
+
style: "accounting"
|
89
|
+
});
|
90
|
+
|
91
|
+
formatter( -1 );
|
92
|
+
// > "($1.00)"
|
93
|
+
```
|
94
|
+
|
95
|
+
For plural messages, use `style: "name"`.
|
96
|
+
|
97
|
+
```javascript
|
98
|
+
var formatter = Globalize( "en" ).currencyFormatter( "USD", {
|
99
|
+
style: "name"
|
100
|
+
});
|
101
|
+
|
102
|
+
formatter( 0 );
|
103
|
+
// > "0.00 US dollars"
|
104
|
+
|
105
|
+
formatter( 1 );
|
106
|
+
// > "1.00 US dollar"
|
107
|
+
```
|
108
|
+
|
109
|
+
For comparison, follow the formatting output of different symbols in different locales using the plural messages `Globalize( locale ).currencyFormatter( currency, { style: "name" } )( 1 )`.
|
110
|
+
|
111
|
+
| 3-letter currency code | en (English) | de (German) | zh (Chinese) |
|
112
|
+
| ---------------------- | ----------------------------- | -------------------------------- | ------------ |
|
113
|
+
| `USD` | `1.00 US dollar` | `1,00 US-Dollar` | `1.00美元` |
|
114
|
+
| `EUR` | `1.00 euro` | `1,00 Euro` | `1.00欧元` |
|
115
|
+
| `CNY` | `1.00 Chinese yuan` | `1,00 Chinesischer Yuan` | `1.00人民币` |
|
116
|
+
| `JPY` | `1 Japanese yen` | `1 Japanischer Yen` | `1日元` |
|
117
|
+
| `GBP` | `1.00 British pound sterling` | `1,00 Britisches Pfund Sterling` | `1.00英镑` |
|
118
|
+
| `BRL` | `1.00 Brazilian real` | `1,00 Brasilianischer Real` | `1.00巴西雷亚尔` |
|
119
|
+
|
120
|
+
For the international currency code, use `style: "code"`.
|
121
|
+
|
122
|
+
```javascript
|
123
|
+
var formatter = Globalize( "en" ).currencyFormatter( "USD", {
|
124
|
+
style: "code"
|
125
|
+
});
|
126
|
+
|
127
|
+
formatter( 9.99 );
|
128
|
+
// > "9.99 USD"
|
129
|
+
```
|
130
|
+
|
131
|
+
#### Configuring inherited number options
|
132
|
+
|
133
|
+
Override the number of digits, grouping separators, rounding function or any other [`.numberFormatter()` options](../number/number-formatter.md).
|
134
|
+
|
135
|
+
```javascript
|
136
|
+
var formatter;
|
137
|
+
|
138
|
+
Globalize.locale( "en" );
|
139
|
+
formatter = Globalize.currencyFormatter( "USD", {
|
140
|
+
minimumFractionDigits: 0,
|
141
|
+
style: "name"
|
142
|
+
});
|
143
|
+
|
144
|
+
formatter( 1 );
|
145
|
+
// > "1 US dollar"
|
146
|
+
|
147
|
+
formatter = Globalize.currencyFormatter( "USD", {
|
148
|
+
round: "ceil"
|
149
|
+
});
|
150
|
+
|
151
|
+
formatter( 1.491 );
|
152
|
+
// > "$1.50"
|
153
|
+
```
|
154
|
+
|
155
|
+
#### Formatting Compact Currencies
|
156
|
+
|
157
|
+
```js
|
158
|
+
var shortFormatter = Globalize( "en" ).currencyFormatter( "USD", {
|
159
|
+
compact: "short"
|
160
|
+
});
|
161
|
+
|
162
|
+
var longFormatter = Globalize( "en" ).currencyFormatter( "USD", {
|
163
|
+
compact: "long"
|
164
|
+
});
|
165
|
+
|
166
|
+
shortFormatter( 12830000000 );
|
167
|
+
// > "$13B"
|
168
|
+
|
169
|
+
longFormatter( 12830000000 );
|
170
|
+
// > "$13 billion"
|
171
|
+
```
|
172
|
+
|
173
|
+
The minimumSignificantDigits and maximumSignificantDigits options are specially useful to control the number of digits to display.
|
174
|
+
|
175
|
+
```js
|
176
|
+
Globalize( "en" ).formatCurrency( 12830000000, "USD", {
|
177
|
+
compact: "short",
|
178
|
+
minimumSignificantDigits: 3,
|
179
|
+
maximumSignificantDigits: 3
|
180
|
+
});
|
181
|
+
// > "$12.8B"
|
182
|
+
```
|
183
|
+
|
184
|
+
#### Performance Suggestion
|
185
|
+
|
186
|
+
For improved performance on iterations, first create the formatter. Then, reuse it on each loop.
|
187
|
+
|
188
|
+
```javascript
|
189
|
+
var formatter = Globalize( "en" ).currencyFormatter( "USD" );
|
190
|
+
|
191
|
+
renderInvoice({
|
192
|
+
prices: prices.map(function( price ) {
|
193
|
+
return formatter( price );
|
194
|
+
})
|
195
|
+
});
|
196
|
+
```
|
@@ -0,0 +1,117 @@
|
|
1
|
+
## .currencyToPartsFormatter( currency [, options] ) ➜ function( value )
|
2
|
+
|
3
|
+
Return a function that formats a `currency` into parts tokens according to the given `options` or locale's defaults.
|
4
|
+
|
5
|
+
The returned function is invoked with one argument: the Number `value` to be formatted.
|
6
|
+
|
7
|
+
### Parameters
|
8
|
+
|
9
|
+
#### currency
|
10
|
+
|
11
|
+
3-letter currency code as defined by ISO 4217, eg. `"USD"`.
|
12
|
+
|
13
|
+
#### options
|
14
|
+
|
15
|
+
Please, see [.currencyFormatter() options](./currency-formatter.md#parameters).
|
16
|
+
|
17
|
+
#### value
|
18
|
+
|
19
|
+
Number to be formatted, eg. `9.99`.
|
20
|
+
|
21
|
+
### Returns
|
22
|
+
|
23
|
+
An Array of objects containing the formatted currency in parts. The returned structure looks like this:
|
24
|
+
|
25
|
+
```js
|
26
|
+
[
|
27
|
+
{ type: "day", value: "17" },
|
28
|
+
{ type: "weekday", value: "Monday" }
|
29
|
+
]
|
30
|
+
```
|
31
|
+
|
32
|
+
Possible types are the following:
|
33
|
+
|
34
|
+
- `currency`
|
35
|
+
|
36
|
+
The currency string, such as the symbols `"$"` and `"€"` or the name `"Dollar"`, `"Euro"` depending on which style is used.
|
37
|
+
|
38
|
+
Please, see [.numberToPartsFormatter()](../number/number-to-parts-formatter.md#returns) for details about the inherited number parts such as `decimal`, `fraction`, `group`, `infinity`, `integer`, `literal`, `minusSign`, `nan`, `plusSign`, `percentSign`, and `compact`.
|
39
|
+
|
40
|
+
### Example
|
41
|
+
|
42
|
+
Prior to using any currency methods, you must load `cldr/main/{locale}/currencies.json`, `cldr/supplemental/currencyData.json`, and the CLDR content required by the number module. If using plural messages, you also must load the CLDR content required by the plural module. Read [CLDR content][] if you need more information.
|
43
|
+
|
44
|
+
[CLDR content]: ../../../README.md#2-cldr-content
|
45
|
+
|
46
|
+
#### Static Formatter
|
47
|
+
|
48
|
+
#### Using the default options
|
49
|
+
|
50
|
+
You can use the static method `Globalize.currencyToPartsFormatter()`, which uses the default locale.
|
51
|
+
|
52
|
+
```javascript
|
53
|
+
var formatter;
|
54
|
+
|
55
|
+
Globalize.locale( "en" );
|
56
|
+
formatter = Globalize.currencyToPartsFormatter( "USD" );
|
57
|
+
|
58
|
+
formatter( 9.99 );
|
59
|
+
// > [
|
60
|
+
// { "type": "currency", "value": "$" },
|
61
|
+
// { "type": "integer", "value": "9" },
|
62
|
+
// { "type": "decimal", "value": "." },
|
63
|
+
// { "type": "fraction", "value": "99" }
|
64
|
+
// ]
|
65
|
+
```
|
66
|
+
|
67
|
+
#### Instance Formatter
|
68
|
+
|
69
|
+
You can use the instance method `.currencyFormatter()`, which uses the instance locale.
|
70
|
+
|
71
|
+
```javascript
|
72
|
+
var deFormatter = Globalize( "de" ).currencyToPartsFormatter( "EUR" ),
|
73
|
+
zhFormatter = Globalize( "zh" ).currencyToPartsFormatter( "EUR" );
|
74
|
+
|
75
|
+
deFormatter( 9.99 );
|
76
|
+
// > [
|
77
|
+
// { "type": "integer", "value": "9" },
|
78
|
+
// { "type": "decimal", "value": "," },
|
79
|
+
// { "type": "fraction", "value": "99" },
|
80
|
+
// { "type": "literal", "value": " " },
|
81
|
+
// { "type": "currency", "value": "€" }
|
82
|
+
// ]
|
83
|
+
|
84
|
+
zhFormatter( 9.99 );
|
85
|
+
// > [
|
86
|
+
// { "type": "currency", "value": "€" },
|
87
|
+
// { "type": "integer", "value": "9" },
|
88
|
+
// { "type": "decimal", "value": "." },
|
89
|
+
// { "type": "fraction", "value": "99" }
|
90
|
+
// ]
|
91
|
+
```
|
92
|
+
|
93
|
+
The information is available separately and it can be formatted and concatenated again in a customized way. For example by using [`Array.prototype.map()`][], [arrow functions][], a [switch statement][], [template literals][], and [`Array.prototype.reduce()`][].
|
94
|
+
|
95
|
+
[`Array.prototype.map()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
|
96
|
+
[arrow functions]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions
|
97
|
+
[switch statement]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch
|
98
|
+
[template literals]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals
|
99
|
+
[`Array.prototype.reduce()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce
|
100
|
+
|
101
|
+
#### More Examples
|
102
|
+
|
103
|
+
Please, see [.currencyFormatter() example](./currency-formatter.md#example) for additional examples such as using alternative `symbolForm`, configuring `style` (symbol, accounting, and name styles), and the inherited number options (e.g., compact numbers).
|
104
|
+
|
105
|
+
#### Performance Suggestion
|
106
|
+
|
107
|
+
For improved performance on iterations, first create the formatter. Then, reuse it on each loop.
|
108
|
+
|
109
|
+
```javascript
|
110
|
+
var formatter = Globalize( "en" ).currencyToPartsFormatter( "USD" );
|
111
|
+
|
112
|
+
renderInvoice({
|
113
|
+
prices: prices.map(function( price ) {
|
114
|
+
return formatter( price );
|
115
|
+
})
|
116
|
+
});
|
117
|
+
```
|
@@ -0,0 +1,203 @@
|
|
1
|
+
## .dateFormatter( [options] ) ➜ function( value )
|
2
|
+
|
3
|
+
Return a function that formats a date according to the given `options`. The default formatting is numeric year, month, and day (i.e., `{ skeleton: "yMd" }`.
|
4
|
+
|
5
|
+
The returned function is invoked with one argument: the Date instance `value` to be formatted.
|
6
|
+
|
7
|
+
### Parameters
|
8
|
+
|
9
|
+
#### options.skeleton
|
10
|
+
|
11
|
+
String value indicating a skeleton (see description above), eg. `{ skeleton: "GyMMMd" }`.
|
12
|
+
|
13
|
+
Skeleton provides a more flexible formatting mechanism than the predefined list `full`, `long`, `medium`, or `short` represented by date, time, or datetime. Instead, they are an open-ended list of patterns containing only date field information, and in a canonical order. For a complete list of skeleton patterns [check the unicode CLDR documentation](http://www.unicode.org/reports/tr35/tr35-dates.html#Date_Field_Symbol_Table).
|
14
|
+
|
15
|
+
For example:
|
16
|
+
|
17
|
+
| locale | `"GyMMMd"` skeleton |
|
18
|
+
| ------ | ------------------------- |
|
19
|
+
| *en* | `"Apr 9, 2014 AD"` |
|
20
|
+
| *zh* | `"公元2014年4月9日"` |
|
21
|
+
| *es* | `"9 abr. de 2014 d. C."` |
|
22
|
+
| *ar* | `"٩ أبريل، ٢٠١٤ م"` |
|
23
|
+
| *pt* | `"9 de abr de 2014 d.C."` |
|
24
|
+
|
25
|
+
#### options.date
|
26
|
+
|
27
|
+
One of the following String values: `full`, `long`, `medium`, or `short`, eg., `{ date: "full" }`.
|
28
|
+
|
29
|
+
#### options.time
|
30
|
+
|
31
|
+
One of the following String values: `full`, `long`, `medium`, or `short`, eg., `{ time: "full" }`.
|
32
|
+
|
33
|
+
#### options.datetime
|
34
|
+
|
35
|
+
One of the following String values: `full`, `long`, `medium`, or `short`, eg., `{ datetime: "full" }`.
|
36
|
+
|
37
|
+
#### options.raw
|
38
|
+
|
39
|
+
String value indicating a machine [raw pattern (anything in the "Sym." column)](http://www.unicode.org/reports/tr35/tr35-dates.html#Date_Field_Symbol_Table) eg. `{ raw: "dd/mm" }`. Note this is NOT recommended for i18n in general. Use `skeleton` instead.
|
40
|
+
|
41
|
+
#### options.timeZone
|
42
|
+
|
43
|
+
String based on the time zone names of the [IANA time zone database](https://www.iana.org/time-zones), such as `"Asia/Shanghai"`, `"Asia/Kolkata"`, `"America/New_York"`.
|
44
|
+
|
45
|
+
#### value
|
46
|
+
|
47
|
+
Date instance to be formatted, eg. `new Date()`;
|
48
|
+
|
49
|
+
### Example
|
50
|
+
|
51
|
+
Prior to using any date methods, you must load `cldr/main/{locale}/ca-gregorian.json`, `cldr/main/{locale}/timeZoneNames.json`, `cldr/supplemental/metaZones.json`, `cldr/supplemental/timeData.json`, `cldr/supplemental/weekData.json`, and the CLDR content required by the number module. Read [CLDR content][] if you need more information.
|
52
|
+
|
53
|
+
[CLDR content]: ../../../README.md#2-cldr-content
|
54
|
+
|
55
|
+
```javascript
|
56
|
+
var formatter;
|
57
|
+
|
58
|
+
Globalize.locale( "en" );
|
59
|
+
formatter = Globalize.dateFormatter();
|
60
|
+
|
61
|
+
formatter( new Date( 2010, 10, 30, 17, 55 ) );
|
62
|
+
// > "11/30/2010"
|
63
|
+
```
|
64
|
+
|
65
|
+
You can use the instance method `.dateFormatter()`, which uses the instance locale.
|
66
|
+
|
67
|
+
```javascript
|
68
|
+
var enFormatter = Globalize( "en" ).dateFormatter(),
|
69
|
+
deFormatter = Globalize( "de" ).dateFormatter();
|
70
|
+
|
71
|
+
enFormatter( new Date( 2010, 10, 30, 17, 55 ) );
|
72
|
+
// > "11/30/2010"
|
73
|
+
|
74
|
+
deFormatter( new Date( 2010, 10, 30, 17, 55 ) );
|
75
|
+
// > "30.11.2010"
|
76
|
+
```
|
77
|
+
|
78
|
+
#### Using short, medium, long, and full presets
|
79
|
+
|
80
|
+
Use convenient presets for `date`, `time`, or `datetime`. Their possible values are: `short`, `medium`, `long`, and `full`.
|
81
|
+
|
82
|
+
| `presetValue` | `Globalize( "en" ).dateFormatter( presetValue )( new Date( 2010, 10, 1, 17, 55 ) )` |
|
83
|
+
| ------------------------ | ---------------------------------------- |
|
84
|
+
| `{ date: "short" }` | `"11/1/10"` |
|
85
|
+
| `{ date: "medium" }` | `"Nov 1, 2010"` |
|
86
|
+
| `{ date: "long" }` | `"November 1, 2010"` |
|
87
|
+
| `{ date: "full" }` | `"Monday, November 1, 2010"` |
|
88
|
+
| `{ time: "short" }` | `"5:55 PM"` |
|
89
|
+
| `{ time: "medium" }` | `"5:55:00 PM"` |
|
90
|
+
| `{ time: "long" }` | `"5:55:00 PM PST"` |
|
91
|
+
| `{ time: "full" }` | `"5:55:00 PM Pacific Standard Time"` |
|
92
|
+
| `{ datetime: "short" }` | `"11/1/10, 5:55 PM"` |
|
93
|
+
| `{ datetime: "medium" }` | `"Nov 1, 2010, 5:55:00 PM"` |
|
94
|
+
| `{ datetime: "long" }` | `"November 1, 2010 at 5:55:00 PM PST"` |
|
95
|
+
| `{ datetime: "full" }` | `"Monday, November 1, 2010 at 5:55:00 PM Pacific Standard Time"` |
|
96
|
+
|
97
|
+
For comparison, follow the same formatter `{ datetime: "short" }` on different locales.
|
98
|
+
|
99
|
+
| locale | `Globalize( locale ).dateFormatter({ datetime: "short" })( new Date( 2010, 10, 1, 17, 55 ) )` |
|
100
|
+
| ---------------- | ---------------------------------------- |
|
101
|
+
| *en* | `"11/1/10, 5:55 PM"` |
|
102
|
+
| *en_GB* | `"01/11/2010 17:55"` |
|
103
|
+
| *zh* | `"10/11/1 下午5:55"` |
|
104
|
+
| *zh-u-nu-native* | `"一〇/一一/一 下午五:五五"` |
|
105
|
+
| *es* | `"1/11/10 17:55"` |
|
106
|
+
| *de* | `"01.11.10 17:55"` |
|
107
|
+
| *pt* | `"01/11/10 17:55"` |
|
108
|
+
| *ar* | `"١/١١/٢٠١٠ ٥،٥٥ م"` |
|
109
|
+
|
110
|
+
#### Using open-ended skeletons
|
111
|
+
|
112
|
+
Use open-ended skeletons for more flexibility (see its description [above](#parameters)). See some examples below.
|
113
|
+
|
114
|
+
| `skeleton` | `Globalize( "en" ).dateFormatter( skeleton )( new Date( 2010, 10, 1, 17, 55 ) )` |
|
115
|
+
| ---------------------------- | ---------------------------------------- |
|
116
|
+
| `{ skeleton: "E" }` | `"Tue"` |
|
117
|
+
| `{ skeleton: "EHm" }` | `"Tue 17:55"` |
|
118
|
+
| `{ skeleton: "EHms" }` | `"Tue 17:55:00"` |
|
119
|
+
| `{ skeleton: "Ed" }` | `"30 Tue"` |
|
120
|
+
| `{ skeleton: "Ehm" }` | `"Tue 5:55 PM"` |
|
121
|
+
| `{ skeleton: "Ehms" }` | `"Tue 5:55:00 PM"` |
|
122
|
+
| `{ skeleton: "Gy" }` | `"2010 AD"` |
|
123
|
+
| `{ skeleton: "GyMMM" }` | `"Nov 2010 AD"` |
|
124
|
+
| `{ skeleton: "GyMMMEd" }` | `"Tue, Nov 30, 2010 AD"` |
|
125
|
+
| `{ skeleton: "GyMMMd" }` | `"Nov 30, 2010 AD"` |
|
126
|
+
| `{ skeleton: "H" }` | `"17"` |
|
127
|
+
| `{ skeleton: "Hm" }` | `"17:55"` |
|
128
|
+
| `{ skeleton: "Hms" }` | `"17:55:00"` |
|
129
|
+
| `{ skeleton: "M" }` | `"11"` |
|
130
|
+
| `{ skeleton: "MEd" }` | `"Tue, 11/30"` |
|
131
|
+
| `{ skeleton: "MMM" }` | `"Nov"` |
|
132
|
+
| `{ skeleton: "MMMEd" }` | `"Tue, Nov 30"` |
|
133
|
+
| `{ skeleton: "MMMd" }` | `"Nov 30"` |
|
134
|
+
| `{ skeleton: "Md" }` | `"11/30"` |
|
135
|
+
| `{ skeleton: "d" }` | `"30"` |
|
136
|
+
| `{ skeleton: "h" }` | `"5 PM"` |
|
137
|
+
| `{ skeleton: "hm" }` | `"5:55 PM"` |
|
138
|
+
| `{ skeleton: "hms" }` | `"5:55:00 PM"` |
|
139
|
+
| `{ skeleton: "ms" }` | `"55:00"` |
|
140
|
+
| `{ skeleton: "y" }` | `"2010"` |
|
141
|
+
| `{ skeleton: "yM" }` | `"11/2010"` |
|
142
|
+
| `{ skeleton: "yMEd" }` | `"Tue, 11/30/2010"` |
|
143
|
+
| `{ skeleton: "yMMM" }` | `"Nov 2010"` |
|
144
|
+
| `{ skeleton: "yMMMEd" }` | `"Tue, Nov 30, 2010"` |
|
145
|
+
| `{ skeleton: "yMMMd" }` | `"Nov 30, 2010"` |
|
146
|
+
| `{ skeleton: "yMd" }` | `"11/30/2010"` |
|
147
|
+
| `{ skeleton: "yQQQ" }` | `"Q4 2010"` |
|
148
|
+
| `{ skeleton: "yQQQQ" }` | `"4th quarter 2010"` |
|
149
|
+
| `{ skeleton: "GyMMMEdhms" }` | `"Tue, Nov 30, 2010 AD, 5:55:00 PM"` |
|
150
|
+
| `{ skeleton: "Ehms" }` | `"Tue 5:55:00 PM"` |
|
151
|
+
| `{ skeleton: "yQQQHm" }` | `"Q4 2010, 17:55"` |
|
152
|
+
| `{ skeleton: "MMMEdhm" }` | `"Tue, Nov 30, 5:55 PM"` |
|
153
|
+
| `{ skeleton: "yMMMdhm" }` | `"Nov 30, 2010, 5:55 PM"` |
|
154
|
+
|
155
|
+
|
156
|
+
```javascript
|
157
|
+
var globalize = Globalize( "en" ),
|
158
|
+
date = new Date( 2010, 10, 30, 17, 55 ),
|
159
|
+
monthDayFormatter = globalize.dateFormatter({ skeleton: "MMMd" }),
|
160
|
+
hourMinuteSecondFormatter = globalize.dateFormatter({ skeleton: "Hms" });
|
161
|
+
|
162
|
+
monthDayFormatter( date );
|
163
|
+
// > "Nov 30"
|
164
|
+
|
165
|
+
hourMinuteSecondFormatter( date );
|
166
|
+
// > "17:55:00"
|
167
|
+
```
|
168
|
+
|
169
|
+
#### Using time zones
|
170
|
+
|
171
|
+
Using specific timeZones, i.e., using `options.timezone`. Note that prior to using it, you must load IANA time zone data.
|
172
|
+
|
173
|
+
```js
|
174
|
+
Globalize.loadTimeZone( require( "iana-tz-data" ) );
|
175
|
+
```
|
176
|
+
|
177
|
+
```js
|
178
|
+
Globalize.locale( "en" );
|
179
|
+
|
180
|
+
Globalize.dateFormatter({ datetime: "medium", timeZone: "America/Los_Angeles" })( new Date() );
|
181
|
+
// > "Nov 1, 2010, 12:55:00 PM"
|
182
|
+
|
183
|
+
Globalize.dateFormatter({ datetime: "medium", timeZone: "America/Sao_Paulo" })( new Date() )
|
184
|
+
// > "Nov 1, 2010, 5:55:00 PM"
|
185
|
+
|
186
|
+
Globalize.dateFormatter({ datetime: "full", timeZone: "Europe/Berlin" })( new Date() )
|
187
|
+
// > "Monday, November 1, 2010 at 8:55:00 PM Central European Standard Time"
|
188
|
+
```
|
189
|
+
|
190
|
+
#### Note on performance
|
191
|
+
|
192
|
+
For improved performance on iterations, first create the formatter. Then, reuse it on each loop.
|
193
|
+
|
194
|
+
```javascript
|
195
|
+
// In an application, this array could have a few hundred entries
|
196
|
+
var dates = [ new Date( 2010, 10, 30, 17, 55 ), new Date( 2015, 3, 18, 4, 25 ) ];
|
197
|
+
var formatter = Globalize( "en" ).dateFormatter({ time: "short" });
|
198
|
+
|
199
|
+
var formattedDates = dates.map(function( date ) {
|
200
|
+
return formatter( date );
|
201
|
+
});
|
202
|
+
// > Array [ "5:55 PM", "4:25 AM" ]
|
203
|
+
```
|
@@ -0,0 +1,60 @@
|
|
1
|
+
## .dateParser( [options] ) ➜ function( value )
|
2
|
+
|
3
|
+
Return a function that parses a string representing a date into a JavaScript Date object according to the given `options`. The default parsing assumes numeric year, month, and day (i.e., `{ skeleton: "yMd" }`).
|
4
|
+
|
5
|
+
The returned function is invoked with one argument: the String `value` to be parsed.
|
6
|
+
|
7
|
+
### Parameters
|
8
|
+
|
9
|
+
#### options
|
10
|
+
|
11
|
+
See [.dateFormatter() options](./date-formatter.md#parameters).
|
12
|
+
|
13
|
+
#### value
|
14
|
+
|
15
|
+
String with date to be parsed, eg. `"11/1/10, 5:55 PM"`.
|
16
|
+
|
17
|
+
### Example
|
18
|
+
|
19
|
+
Prior to using any date methods, you must load `cldr/main/{locale}/ca-gregorian.json`, `cldr/main/{locale}/timeZoneNames.json`, `cldr/supplemental/timeData.json`, `cldr/supplemental/weekData.json`, and the CLDR content required by the number module. Read [CLDR content][] if you need more information.
|
20
|
+
|
21
|
+
[CLDR content]: ../../../README.md#2-cldr-content
|
22
|
+
|
23
|
+
You can use the static method `Globalize.dateParser()`, which uses the default locale.
|
24
|
+
|
25
|
+
```javascript
|
26
|
+
var parser;
|
27
|
+
|
28
|
+
Globalize.locale( "en" );
|
29
|
+
parser = Globalize.dateParser();
|
30
|
+
|
31
|
+
parser( "1/2/2013" );
|
32
|
+
// > Wed Jan 02 2013 00:00:00
|
33
|
+
|
34
|
+
Globalize.locale( "es" );
|
35
|
+
parser = Globalize.dateParser();
|
36
|
+
|
37
|
+
parser( "1/2/2013" );
|
38
|
+
// > Fri Feb 01 2013 00:00:00
|
39
|
+
```
|
40
|
+
|
41
|
+
You can use the instance method `.dateParser()`, which uses the instance locale.
|
42
|
+
|
43
|
+
```javascript
|
44
|
+
var esParser = Globalize( "es" ).dateParser({ date: short });
|
45
|
+
|
46
|
+
esParser( "1/2/13" );
|
47
|
+
// > Fri Feb 01 2013 00:00:00
|
48
|
+
```
|
49
|
+
|
50
|
+
For improved performance on iterations, first create the parser. Then, reuse it
|
51
|
+
on each loop.
|
52
|
+
|
53
|
+
```javascript
|
54
|
+
var formattedDates = [ new Date( a ), new Date( b ), ... ];
|
55
|
+
var parser = Globalize( "en" ).dateParser({ time: "short" });
|
56
|
+
|
57
|
+
dates = formattedDates.map(function( formattedDate ) {
|
58
|
+
return parser( formattedDate );
|
59
|
+
});
|
60
|
+
```
|