|
1 |
| -# Eleventy’s ISO8601 Date parser |
| 1 | +# Eleventy ISO8601 Date Parser |
2 | 2 |
|
3 | 3 | Features:
|
4 | 4 |
|
5 | 5 | - Zero dependency super minimal ISO8601 date parsing library. Alternatives were [too lax, inaccurate, huge on disk, or huge in bundle](https://fediverse.zachleat.com/@zachleat/114870836413532617).
|
6 |
| -- Dates with 8 digits do not require `-` delimiters (all other dates require delimiters, e.g. `YYYY-MM`) |
7 |
| -- Times with 6 digits do not require `:` delimiters (all others times require delimiters, e.g. `HH:II`) |
8 |
| -- Requires the `T` delimiter for date and time |
9 |
| -- Defaults to UTC when time zone is unknown instead of local time (`Z` can be explicit or implied) |
| 6 | +- All dates supported by this library are RFC-9557 compatible (parseable by `Temporal.Instant.from` or `Temporal.PlainDateTime.from`) to prepare for wider [Temporal API](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal) support. |
| 7 | + - Note that this is not a full polyfill — not all RFC-9557 dates are supported here. Alternatives: [`temporal-polyfill`](https://github.com/fullcalendar/temporal-polyfill) or [`js-temporal/temporal-polyfill`](https://github.com/js-temporal/temporal-polyfill) |
| 8 | +- Defaults to UTC when time zone is unknown instead of local time |
10 | 9 | - This matches previous behavior in Eleventy and this feature maintains consistency between build and deploy servers.
|
11 | 10 | - Supports +00:00 or -00:00 style time zone offsets
|
12 | 11 | - Invalid strings throw errors.
|
| 12 | +- Delimiter notes: |
| 13 | + - Requires the `T` delimiter for Date and Time |
| 14 | + - Dates with 8 digits do not require `-` delimiters (all other dates require delimiters, e.g. `YYYY-MM`) |
| 15 | + - Times with 6 digits do not require `:` delimiters (all others times require delimiters, e.g. `HH:II`) |
13 | 16 |
|
14 |
| -Not supported: |
| 17 | +Not supported (for RFC 9557 compatibility): |
15 | 18 |
|
16 |
| -- Standalone times are not supported (must be date or datetime) |
17 |
| -- [ISO week date syntax](https://en.wikipedia.org/wiki/ISO_week_date) (e.g. `YYYY-W01-1`) |
18 |
| -- Day of year syntax is not supported (e.g. `YYYY-001`, `YYYY-365`) |
19 |
| -- Fractional syntax for hours or minutes (e.g. `T14.6` or `T10:30.6`). Fractional seconds (e.g. milliseconds) are supported (of course). |
| 19 | +- Standalone time formats are *not* supported (must be Date or DateTime) |
| 20 | +- [ISO week date syntax](https://en.wikipedia.org/wiki/ISO_week_date) is *not* supported (e.g. `YYYY-W01-1`) |
| 21 | +- Day of year syntax is *not* supported (e.g. `YYYY-001`, `YYYY-365`) |
| 22 | +- Fractional syntax for hours or minutes (e.g. `T14.6` or `T10:30.6`) is *not* supported. Fractional seconds (e.g. milliseconds) are supported (of course). |
20 | 23 |
|
21 |
| -## `luxon` Comparison |
| 24 | +## Usage |
| 25 | + |
| 26 | +``` |
| 27 | +npm install @11ty/parse-date-strings |
| 28 | +``` |
| 29 | + |
| 30 | +```js |
| 31 | +import { parse } from "@11ty/parse-date-strings |
| 32 | +
|
| 33 | +// returns JavaScript Date object |
| 34 | +parse("2000-01-01") instanceof Date |
| 35 | +> true |
| 36 | +
|
| 37 | +// Convert to UTC String |
| 38 | +parse("2000-01-01").toUTCString() |
| 39 | +> "Mon, 01 Jan 2001 00:00:00 GMT" |
| 40 | +``` |
| 41 | +
|
| 42 | +## Comparisons |
| 43 | +
|
| 44 | +### `luxon` |
22 | 45 |
|
23 | 46 | More strict parsing compared with [Luxon’s `fromISO`](https://moment.github.io/luxon/#/parsing?id=iso-8601) (used in Eleventy v0.x through v3):
|
24 | 47 |
|
25 | 48 | ```
|
26 |
| -2016 |
27 |
| -2016-05 |
| 49 | +2016 # Dropped |
| 50 | +2016-05 # Dropped |
28 | 51 | 201605 # Dropped, delimiter required if date is not 8 digits
|
29 | 52 | 2016-05-25
|
30 | 53 | 20160525
|
|
0 commit comments