parse.d.cts 23 KB

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