PatternRegex

Match a date in YYYY-MM-DD

An ISO-style date pattern that actually checks the calendar shape — month 13 and day 32 are rejected, unlike the naive \d{4}-\d{2}-\d{2}.

Last updated

Patterng
\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])
Open in Regex Tester & Builder

Matches

  • 2026-09-04
  • 1999-12-31
  • 2030-01-09

Doesn’t match

  • 2026-13-01
  • 2026-00-12
  • 26-09-04

Why it’s written this way

The month group allows 01–09 or 10–12, and the day group allows 01–09, 10–29, or 30–31 — so the pattern encodes the shape of a calendar, not just "digits with dashes".

That is the practical middle ground: \d{4}-\d{2}-\d{2} happily accepts 2026-13-99, while a pattern that also knew February's length would be unreadable and still wrong every leap year.

Edge cases to know

  • It accepts 2026-02-31 — per-month day counts are a job for a date parser, not a regex.
  • It matches the date inside a longer timestamp (2026-09-04T10:00) — anchor with ^…$ to validate a whole field.
  • Years are unrestricted: 0000 and 9999 both pass.

Related in Patterns