Regex for date in YYYY-MM-DD format

This pattern matches ISO 8601 calendar dates and restricts months to 01–12 and days to 01–31. It is a good first filter before parsing the value as a real date.

The pattern

/\b\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])\b/g

The tester opens with this pattern and the examples below already loaded. Nothing you type is uploaded.

How it works

\d{4}
Four-digit year.
(?:0[1-9]|1[0-2])
Month 01 to 12.
(?:0[1-9]|[12]\d|3[01])
Day 01 to 31.

Examples

Matches

  • 2026-10-02
  • 1999-12-31
  • 2024-02-29

Does not match

  • 2026-13-01
  • 2026-00-10
  • 26-10-02
  • 2026/10/02

Use it in JavaScript

const re = /\b\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])\b/g;

// Find every match in a block of text
const found = text.match(re);

// Validate that a whole value matches (no stray characters around it)
const exact = new RegExp('^(?:' + re.source + ')$', re.flags.replace('g', ''));
const isValid = exact.test(value);

Limits and tips

  • It does not know how many days each month has, so 2026-02-31 still matches. Check real calendar validity by parsing the date in code.
  • Leap years cannot be handled sensibly with a regex. Use Date or a date library after the format check.

Frequently asked questions

Does it reject February 30th?

No. The pattern allows days up to 31 in every month. Parse the date in code to catch impossible dates.

Can I accept slashes as well as dashes?

Yes. Replace each - with [-\/] to accept either separator.