UNPKG

22.8 kBTypeScriptView Raw
1import type {
2 AdditionalTokensOptions,
3 FirstWeekContainsDateOptions,
4 LocalizedOptions,
5 WeekOptions,
6} from "./types.js";
7import { longFormatters } from "./_lib/format/longFormatters.js";
8import { parsers } from "./parse/_lib/parsers.js";
9export { longFormatters, parsers };
10/**
11 * The {@link parse} function options.
12 */
13export interface ParseOptions
14 extends LocalizedOptions<"options" | "match" | "formatLong">,
15 FirstWeekContainsDateOptions,
16 WeekOptions,
17 AdditionalTokensOptions {}
18/**
19 * @name parse
20 * @category Common Helpers
21 * @summary Parse the date.
22 *
23 * @description
24 * Return the date parsed from string using the given format string.
25 *
26 * > ⚠️ Please note that the `format` tokens differ from Moment.js and other libraries.
27 * > See: https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.md
28 *
29 * The characters in the format string wrapped between two single quotes characters (') are escaped.
30 * Two single quotes in a row, whether inside or outside a quoted sequence, represent a 'real' single quote.
31 *
32 * Format of the format string is based on Unicode Technical Standard #35:
33 * https://www.unicode.org/reports/tr35/tr35-dates.html#Date_Field_Symbol_Table
34 * with a few additions (see note 5 below the table).
35 *
36 * Not all tokens are compatible. Combinations that don't make sense or could lead to bugs are prohibited
37 * and will throw `RangeError`. For example usage of 24-hour format token with AM/PM token will throw an exception:
38 *
39 * ```javascript
40 * parse('23 AM', 'HH a', new Date())
41 * //=> RangeError: The format string mustn't contain `HH` and `a` at the same time
42 * ```
43 *
44 * See the compatibility table: https://docs.google.com/spreadsheets/d/e/2PACX-1vQOPU3xUhplll6dyoMmVUXHKl_8CRDs6_ueLmex3SoqwhuolkuN3O05l4rqx5h1dKX8eb46Ul-CCSrq/pubhtml?gid=0&single=true
45 *
46 * Accepted format string patterns:
47 * | Unit |Prior| Pattern | Result examples | Notes |
48 * |---------------------------------|-----|---------|-----------------------------------|-------|
49 * | Era | 140 | G..GGG | AD, BC | |
50 * | | | GGGG | Anno Domini, Before Christ | 2 |
51 * | | | GGGGG | A, B | |
52 * | Calendar year | 130 | y | 44, 1, 1900, 2017, 9999 | 4 |
53 * | | | yo | 44th, 1st, 1900th, 9999999th | 4,5 |
54 * | | | yy | 44, 01, 00, 17 | 4 |
55 * | | | yyy | 044, 001, 123, 999 | 4 |
56 * | | | yyyy | 0044, 0001, 1900, 2017 | 4 |
57 * | | | yyyyy | ... | 2,4 |
58 * | Local week-numbering year | 130 | Y | 44, 1, 1900, 2017, 9000 | 4 |
59 * | | | Yo | 44th, 1st, 1900th, 9999999th | 4,5 |
60 * | | | YY | 44, 01, 00, 17 | 4,6 |
61 * | | | YYY | 044, 001, 123, 999 | 4 |
62 * | | | YYYY | 0044, 0001, 1900, 2017 | 4,6 |
63 * | | | YYYYY | ... | 2,4 |
64 * | ISO week-numbering year | 130 | R | -43, 1, 1900, 2017, 9999, -9999 | 4,5 |
65 * | | | RR | -43, 01, 00, 17 | 4,5 |
66 * | | | RRR | -043, 001, 123, 999, -999 | 4,5 |
67 * | | | RRRR | -0043, 0001, 2017, 9999, -9999 | 4,5 |
68 * | | | RRRRR | ... | 2,4,5 |
69 * | Extended year | 130 | u | -43, 1, 1900, 2017, 9999, -999 | 4 |
70 * | | | uu | -43, 01, 99, -99 | 4 |
71 * | | | uuu | -043, 001, 123, 999, -999 | 4 |
72 * | | | uuuu | -0043, 0001, 2017, 9999, -9999 | 4 |
73 * | | | uuuuu | ... | 2,4 |
74 * | Quarter (formatting) | 120 | Q | 1, 2, 3, 4 | |
75 * | | | Qo | 1st, 2nd, 3rd, 4th | 5 |
76 * | | | QQ | 01, 02, 03, 04 | |
77 * | | | QQQ | Q1, Q2, Q3, Q4 | |
78 * | | | QQQQ | 1st quarter, 2nd quarter, ... | 2 |
79 * | | | QQQQQ | 1, 2, 3, 4 | 4 |
80 * | Quarter (stand-alone) | 120 | q | 1, 2, 3, 4 | |
81 * | | | qo | 1st, 2nd, 3rd, 4th | 5 |
82 * | | | qq | 01, 02, 03, 04 | |
83 * | | | qqq | Q1, Q2, Q3, Q4 | |
84 * | | | qqqq | 1st quarter, 2nd quarter, ... | 2 |
85 * | | | qqqqq | 1, 2, 3, 4 | 3 |
86 * | Month (formatting) | 110 | M | 1, 2, ..., 12 | |
87 * | | | Mo | 1st, 2nd, ..., 12th | 5 |
88 * | | | MM | 01, 02, ..., 12 | |
89 * | | | MMM | Jan, Feb, ..., Dec | |
90 * | | | MMMM | January, February, ..., December | 2 |
91 * | | | MMMMM | J, F, ..., D | |
92 * | Month (stand-alone) | 110 | L | 1, 2, ..., 12 | |
93 * | | | Lo | 1st, 2nd, ..., 12th | 5 |
94 * | | | LL | 01, 02, ..., 12 | |
95 * | | | LLL | Jan, Feb, ..., Dec | |
96 * | | | LLLL | January, February, ..., December | 2 |
97 * | | | LLLLL | J, F, ..., D | |
98 * | Local week of year | 100 | w | 1, 2, ..., 53 | |
99 * | | | wo | 1st, 2nd, ..., 53th | 5 |
100 * | | | ww | 01, 02, ..., 53 | |
101 * | ISO week of year | 100 | I | 1, 2, ..., 53 | 5 |
102 * | | | Io | 1st, 2nd, ..., 53th | 5 |
103 * | | | II | 01, 02, ..., 53 | 5 |
104 * | Day of month | 90 | d | 1, 2, ..., 31 | |
105 * | | | do | 1st, 2nd, ..., 31st | 5 |
106 * | | | dd | 01, 02, ..., 31 | |
107 * | Day of year | 90 | D | 1, 2, ..., 365, 366 | 7 |
108 * | | | Do | 1st, 2nd, ..., 365th, 366th | 5 |
109 * | | | DD | 01, 02, ..., 365, 366 | 7 |
110 * | | | DDD | 001, 002, ..., 365, 366 | |
111 * | | | DDDD | ... | 2 |
112 * | Day of week (formatting) | 90 | E..EEE | Mon, Tue, Wed, ..., Sun | |
113 * | | | EEEE | Monday, Tuesday, ..., Sunday | 2 |
114 * | | | EEEEE | M, T, W, T, F, S, S | |
115 * | | | EEEEEE | Mo, Tu, We, Th, Fr, Sa, Su | |
116 * | ISO day of week (formatting) | 90 | i | 1, 2, 3, ..., 7 | 5 |
117 * | | | io | 1st, 2nd, ..., 7th | 5 |
118 * | | | ii | 01, 02, ..., 07 | 5 |
119 * | | | iii | Mon, Tue, Wed, ..., Sun | 5 |
120 * | | | iiii | Monday, Tuesday, ..., Sunday | 2,5 |
121 * | | | iiiii | M, T, W, T, F, S, S | 5 |
122 * | | | iiiiii | Mo, Tu, We, Th, Fr, Sa, Su | 5 |
123 * | Local day of week (formatting) | 90 | e | 2, 3, 4, ..., 1 | |
124 * | | | eo | 2nd, 3rd, ..., 1st | 5 |
125 * | | | ee | 02, 03, ..., 01 | |
126 * | | | eee | Mon, Tue, Wed, ..., Sun | |
127 * | | | eeee | Monday, Tuesday, ..., Sunday | 2 |
128 * | | | eeeee | M, T, W, T, F, S, S | |
129 * | | | eeeeee | Mo, Tu, We, Th, Fr, Sa, Su | |
130 * | Local day of week (stand-alone) | 90 | c | 2, 3, 4, ..., 1 | |
131 * | | | co | 2nd, 3rd, ..., 1st | 5 |
132 * | | | cc | 02, 03, ..., 01 | |
133 * | | | ccc | Mon, Tue, Wed, ..., Sun | |
134 * | | | cccc | Monday, Tuesday, ..., Sunday | 2 |
135 * | | | ccccc | M, T, W, T, F, S, S | |
136 * | | | cccccc | Mo, Tu, We, Th, Fr, Sa, Su | |
137 * | AM, PM | 80 | a..aaa | AM, PM | |
138 * | | | aaaa | a.m., p.m. | 2 |
139 * | | | aaaaa | a, p | |
140 * | AM, PM, noon, midnight | 80 | b..bbb | AM, PM, noon, midnight | |
141 * | | | bbbb | a.m., p.m., noon, midnight | 2 |
142 * | | | bbbbb | a, p, n, mi | |
143 * | Flexible day period | 80 | B..BBB | at night, in the morning, ... | |
144 * | | | BBBB | at night, in the morning, ... | 2 |
145 * | | | BBBBB | at night, in the morning, ... | |
146 * | Hour [1-12] | 70 | h | 1, 2, ..., 11, 12 | |
147 * | | | ho | 1st, 2nd, ..., 11th, 12th | 5 |
148 * | | | hh | 01, 02, ..., 11, 12 | |
149 * | Hour [0-23] | 70 | H | 0, 1, 2, ..., 23 | |
150 * | | | Ho | 0th, 1st, 2nd, ..., 23rd | 5 |
151 * | | | HH | 00, 01, 02, ..., 23 | |
152 * | Hour [0-11] | 70 | K | 1, 2, ..., 11, 0 | |
153 * | | | Ko | 1st, 2nd, ..., 11th, 0th | 5 |
154 * | | | KK | 01, 02, ..., 11, 00 | |
155 * | Hour [1-24] | 70 | k | 24, 1, 2, ..., 23 | |
156 * | | | ko | 24th, 1st, 2nd, ..., 23rd | 5 |
157 * | | | kk | 24, 01, 02, ..., 23 | |
158 * | Minute | 60 | m | 0, 1, ..., 59 | |
159 * | | | mo | 0th, 1st, ..., 59th | 5 |
160 * | | | mm | 00, 01, ..., 59 | |
161 * | Second | 50 | s | 0, 1, ..., 59 | |
162 * | | | so | 0th, 1st, ..., 59th | 5 |
163 * | | | ss | 00, 01, ..., 59 | |
164 * | Seconds timestamp | 40 | t | 512969520 | |
165 * | | | tt | ... | 2 |
166 * | Fraction of second | 30 | S | 0, 1, ..., 9 | |
167 * | | | SS | 00, 01, ..., 99 | |
168 * | | | SSS | 000, 001, ..., 999 | |
169 * | | | SSSS | ... | 2 |
170 * | Milliseconds timestamp | 20 | T | 512969520900 | |
171 * | | | TT | ... | 2 |
172 * | Timezone (ISO-8601 w/ Z) | 10 | X | -08, +0530, Z | |
173 * | | | XX | -0800, +0530, Z | |
174 * | | | XXX | -08:00, +05:30, Z | |
175 * | | | XXXX | -0800, +0530, Z, +123456 | 2 |
176 * | | | XXXXX | -08:00, +05:30, Z, +12:34:56 | |
177 * | Timezone (ISO-8601 w/o Z) | 10 | x | -08, +0530, +00 | |
178 * | | | xx | -0800, +0530, +0000 | |
179 * | | | xxx | -08:00, +05:30, +00:00 | 2 |
180 * | | | xxxx | -0800, +0530, +0000, +123456 | |
181 * | | | xxxxx | -08:00, +05:30, +00:00, +12:34:56 | |
182 * | Long localized date | NA | P | 05/29/1453 | 5,8 |
183 * | | | PP | May 29, 1453 | |
184 * | | | PPP | May 29th, 1453 | |
185 * | | | PPPP | Sunday, May 29th, 1453 | 2,5,8 |
186 * | Long localized time | NA | p | 12:00 AM | 5,8 |
187 * | | | pp | 12:00:00 AM | |
188 * | Combination of date and time | NA | Pp | 05/29/1453, 12:00 AM | |
189 * | | | PPpp | May 29, 1453, 12:00:00 AM | |
190 * | | | PPPpp | May 29th, 1453 at ... | |
191 * | | | PPPPpp | Sunday, May 29th, 1453 at ... | 2,5,8 |
192 * Notes:
193 * 1. "Formatting" units (e.g. formatting quarter) in the default en-US locale
194 * are the same as "stand-alone" units, but are different in some languages.
195 * "Formatting" units are declined according to the rules of the language
196 * in the context of a date. "Stand-alone" units are always nominative singular.
197 * In `format` function, they will produce different result:
198 *
199 * `format(new Date(2017, 10, 6), 'do LLLL', {locale: cs}) //=> '6. listopad'`
200 *
201 * `format(new Date(2017, 10, 6), 'do MMMM', {locale: cs}) //=> '6. listopadu'`
202 *
203 * `parse` will try to match both formatting and stand-alone units interchangably.
204 *
205 * 2. Any sequence of the identical letters is a pattern, unless it is escaped by
206 * the single quote characters (see below).
207 * If the sequence is longer than listed in table:
208 * - for numerical units (`yyyyyyyy`) `parse` will try to match a number
209 * as wide as the sequence
210 * - for text units (`MMMMMMMM`) `parse` will try to match the widest variation of the unit.
211 * These variations are marked with "2" in the last column of the table.
212 *
213 * 3. `QQQQQ` and `qqqqq` could be not strictly numerical in some locales.
214 * These tokens represent the shortest form of the quarter.
215 *
216 * 4. The main difference between `y` and `u` patterns are B.C. years:
217 *
218 * | Year | `y` | `u` |
219 * |------|-----|-----|
220 * | AC 1 | 1 | 1 |
221 * | BC 1 | 1 | 0 |
222 * | BC 2 | 2 | -1 |
223 *
224 * Also `yy` will try to guess the century of two digit year by proximity with `referenceDate`:
225 *
226 * `parse('50', 'yy', new Date(2018, 0, 1)) //=> Sat Jan 01 2050 00:00:00`
227 *
228 * `parse('75', 'yy', new Date(2018, 0, 1)) //=> Wed Jan 01 1975 00:00:00`
229 *
230 * while `uu` will just assign the year as is:
231 *
232 * `parse('50', 'uu', new Date(2018, 0, 1)) //=> Sat Jan 01 0050 00:00:00`
233 *
234 * `parse('75', 'uu', new Date(2018, 0, 1)) //=> Tue Jan 01 0075 00:00:00`
235 *
236 * The same difference is true for local and ISO week-numbering years (`Y` and `R`),
237 * except local week-numbering years are dependent on `options.weekStartsOn`
238 * and `options.firstWeekContainsDate` (compare [setISOWeekYear](https://date-fns.org/docs/setISOWeekYear)
239 * and [setWeekYear](https://date-fns.org/docs/setWeekYear)).
240 *
241 * 5. These patterns are not in the Unicode Technical Standard #35:
242 * - `i`: ISO day of week
243 * - `I`: ISO week of year
244 * - `R`: ISO week-numbering year
245 * - `o`: ordinal number modifier
246 * - `P`: long localized date
247 * - `p`: long localized time
248 *
249 * 6. `YY` and `YYYY` tokens represent week-numbering years but they are often confused with years.
250 * You should enable `options.useAdditionalWeekYearTokens` to use them. See: https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.md
251 *
252 * 7. `D` and `DD` tokens represent days of the year but they are ofthen confused with days of the month.
253 * You should enable `options.useAdditionalDayOfYearTokens` to use them. See: https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.md
254 *
255 * 8. `P+` tokens do not have a defined priority since they are merely aliases to other tokens based
256 * on the given locale.
257 *
258 * using `en-US` locale: `P` => `MM/dd/yyyy`
259 * using `en-US` locale: `p` => `hh:mm a`
260 * using `pt-BR` locale: `P` => `dd/MM/yyyy`
261 * using `pt-BR` locale: `p` => `HH:mm`
262 *
263 * Values will be assigned to the date in the descending order of its unit's priority.
264 * Units of an equal priority overwrite each other in the order of appearance.
265 *
266 * If no values of higher priority are parsed (e.g. when parsing string 'January 1st' without a year),
267 * the values will be taken from 3rd argument `referenceDate` which works as a context of parsing.
268 *
269 * `referenceDate` must be passed for correct work of the function.
270 * If you're not sure which `referenceDate` to supply, create a new instance of Date:
271 * `parse('02/11/2014', 'MM/dd/yyyy', new Date())`
272 * In this case parsing will be done in the context of the current date.
273 * If `referenceDate` is `Invalid Date` or a value not convertible to valid `Date`,
274 * then `Invalid Date` will be returned.
275 *
276 * The result may vary by locale.
277 *
278 * If `formatString` matches with `dateString` but does not provides tokens, `referenceDate` will be returned.
279 *
280 * If parsing failed, `Invalid Date` will be returned.
281 * Invalid Date is a Date, whose time value is NaN.
282 * Time value of Date: http://es5.github.io/#x15.9.1.1
283 *
284 * @typeParam DateType - The `Date` type, the function operates on. Gets inferred from passed arguments. Allows to use extensions like [`UTCDate`](https://github.com/date-fns/utc).
285 *
286 * @param dateStr - The string to parse
287 * @param formatStr - The string of tokens
288 * @param referenceDate - defines values missing from the parsed dateString
289 * @param options - An object with options.
290 * see: https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.md
291 * see: https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.md
292 *
293 * @returns The parsed date
294 *
295 * @throws `options.locale` must contain `match` property
296 * @throws use `yyyy` instead of `YYYY` for formatting years using [format provided] to the input [input provided]; see: https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.md
297 * @throws use `yy` instead of `YY` for formatting years using [format provided] to the input [input provided]; see: https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.md
298 * @throws use `d` instead of `D` for formatting days of the month using [format provided] to the input [input provided]; see: https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.md
299 * @throws use `dd` instead of `DD` for formatting days of the month using [format provided] to the input [input provided]; see: https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.md
300 * @throws format string contains an unescaped latin alphabet character
301 *
302 * @example
303 * // Parse 11 February 2014 from middle-endian format:
304 * var result = parse('02/11/2014', 'MM/dd/yyyy', new Date())
305 * //=> Tue Feb 11 2014 00:00:00
306 *
307 * @example
308 * // Parse 28th of February in Esperanto locale in the context of 2010 year:
309 * import eo from 'date-fns/locale/eo'
310 * var result = parse('28-a de februaro', "do 'de' MMMM", new Date(2010, 0, 1), {
311 * locale: eo
312 * })
313 * //=> Sun Feb 28 2010 00:00:00
314 */
315export declare function parse<DateType extends Date>(
316 dateStr: string,
317 formatStr: string,
318 referenceDate: DateType | number | string,
319 options?: ParseOptions,
320): DateType;