Match ISO 8601 dates with regex
A regex for the calendar-date form of ISO 8601, with named groups for year, month, and day. Notes on full ISO 8601 (timestamp + timezone) and what regex cannot validate.
# pattern
/(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/g → Open in regex tester (pre-filled)
# how it works
Three named capture groups: year (4 digits), month (2 digits), day (2 digits). Use back-references `$<year>`, `$<month>`, `$<day>` in replacements.
# sample input
Born 1991-07-04. Released on 2026-05-14 and patched 2026-06-01. Not a date: 26/05/2026.
# pitfalls
- Does not validate the date is real. "1999-02-30" matches but is impossible. Always parse with Date.UTC(year, month-1, day) and check for NaN.
- Full ISO 8601 timestamps add time and timezone: append `T(\d{2}):(\d{2}):(\d{2})(?:\.\d+)?(Z|[+-]\d{2}:?\d{2})`.
- Some date strings are RFC 3339 (the strict ISO subset) and some are just "YYYY-MM-DD"-ish. Decide which you accept.