UNPKG

1.66 kBMarkdownView Raw
1# Unicode Tokens
2
3Starting with v2 `format` and `parse` uses [Unicode tokens].
4
5The tokens are different from Moment.js and other libraries that opted to use
6custom formatting rules. While usage of a standard ensures compatibility and
7the future of the library it causes confusion that this document intended
8to resolve.
9
10## Popular mistakes
11
12There are 4 tokens that causes the most of confusion:
13
14- `D` and `DD` that represent the day of a year (1, 2, ..., 365, 366)
15 are often confused with `d` and `dd` that represent the day of a month
16 (1, 2, ..., 31).
17
18- `YY` and `YYYY` that represent the local week-numbering year (44, 01, 00, 17)
19 are often confused with `yy` and `yyyy` that represent the the calendar year.
20
21```js
22// ❌ Wrong!
23format(new Date(), 'YYYY-MM-DD')
24//=> 2018-10-283
25
26// ✅ Correct
27format(new Date(), 'yyyy-MM-dd')
28//=> 2018-10-10
29
30// ❌ Wrong!
31parse('11.02.87', 'D.MM.YY', new Date()).toString()
32//=> 'Sat Jan 11 1986 00:00:00 GMT+0200 (EET)'
33
34// ✅ Correct
35parse('11.02.87', 'd.MM.yy', new Date()).toString()
36//=> 'Wed Feb 11 1987 00:00:00 GMT+0200 (EET)'
37```
38
39To help with the issue, `format` and `parse` functions won't accept
40these tokens without `useAdditionalDayOfYearTokens` option for `D` and `DD` and
41`useAdditionalWeekYearTokens` options for `YY` and `YYYY`:
42
43```js
44format(new Date(), 'D', { useAdditionalDayOfYearTokens: true })
45//=> '283'
46
47parse('365+1987', 'DD+YYYY', new Date(), {
48 useAdditionalDayOfYearTokens: true,
49 useAdditionalWeekYearTokens: true
50}).toString()
51//=> 'Wed Dec 31 1986 00:00:00 GMT+0200 (EET)'
52```
53
54[Unicode tokens]: https://www.unicode.org/reports/tr35/tr35-dates.html#Date_Field_Symbol_Table
\No newline at end of file