Features:
- Zero dependency super minimal ISO8601 date parsing library. Alternatives were too lax, inaccurate, huge on disk, or huge in bundle.
- All dates supported by this library are RFC-9557 compatible (parseable by
Temporal.PlainDate.from
,Temporal.Instant.from
, orTemporal.PlainDateTime.from
) to prepare for wider Temporal API support.- Note that this is not a full polyfill — not all RFC-9557 dates are supported here. Alternatives:
temporal-polyfill
orjs-temporal/temporal-polyfill
- Note that this is not a full polyfill — not all RFC-9557 dates are supported here. Alternatives:
- Invalid strings throw errors.
- Time zone notes:
- Defaults to UTC when time zone is unknown (not local time). This matches previous behavior in Eleventy and this feature maintains consistency between build and deploy servers.
- Supports
+00
,+00:00
,-00
, or-00:00
style time zone offsets (:
delimiter is optional)
- Delimiter notes:
- Requires the
T
delimiter with DateTime strings - Delimiters in dates (
-
) and times (:
) are optional
- Requires the
Not supported (for RFC 9557 compatibility):
- Standalone time formats are not supported (must be Date or DateTime)
- ISO week date syntax is not supported (e.g.
YYYY-W01-1
) - Day of year syntax is not supported (e.g.
YYYY-001
,YYYY-365
) - Fractional hours or minutes (e.g.
T14.6
orT10:30.6
) are not supported. Fractional seconds (e.g. milliseconds) are supported (of course).
npm install @11ty/parse-date-strings
import { parse } from "@11ty/parse-date-strings
// `parse` returns JavaScript Date
parse("2000-01-01") instanceof Date
// true
parse("2000-01-01").toUTCString()
// "Mon, 01 Jan 2001 00:00:00 GMT"
More strict parsing compared with Luxon’s fromISO
(used in Eleventy v0.x through v3):
2016-05-25
20160525
2016-05-25T09
2016-05-25T09:24
2016-05-25T09:24:15
2016-05-25T09:24:15.123
2016-05-25T0924
2016-05-25T092415
2016-05-25T092415.123
2016-05-25T09:24:15,123
# No YYYY or YYYYMM syntax
2016 # Dropped
2016-05 # Dropped
201605 # Dropped
# No ISO week date syntax
2016-W21-3 # Dropped
2016W213 # Dropped
2016-W21-3T09:24:15.123 # Dropped
2016W213T09:24:15.123 # Dropped
# No day of year syntax (e.g. 200th day of the year)
2016-200 # Dropped
2016200 # Dropped
2016-200T09:24:15.123 # Dropped
# No implied current day (time-only syntax)
09:24 # Dropped
09:24:15 # Dropped
09:24:15.123 # Dropped
09:24:15,123 # Dropped