Developer

Encoders, converters & dev utilities

12 tools
Developer · Regex Cheat Sheet & Common Patterns

Regex to Match a Date — YYYY-MM-DD (ISO 8601)

To match an ISO 8601 date in YYYY-MM-DD form, use ^\d{4}-\d{2}-\d{2}$ — four digits, a hyphen, two digits, a hyphen, two digits. This is the single most useful date pattern because ISO 8601 is unambiguous and sorts correctly as text. The crucial caveat: this validates the shape only. It happily matches 2026-13-40, because a regex cannot easily check that the month is 1–12 and the day is valid for that specific month and year (leap years included). Regex is the wrong tool for that kind of numeric range and calendar validation. The correct workflow is to use this pattern to confirm the format, then parse the value with a real date library (or the Date object) to confirm it is a legal calendar date. This JavaScript pattern is verified in CI. Copy it, test your own strings in the live in-browser tester, and range-check in code. Free, no login.

Quick answer

Pattern: ^\d{4}-\d{2}-\d{2}$ (ISO 8601 date shape)

  • Matches: 2026-07-04
  • Rejects: 2026-7-4 (missing zero-padding), 07/04/2026 and 2026/07/04 (wrong separators)
  • Shape only — it also accepts 2026-13-40, so range-check the month and day in code
  • Parse with a date library afterwards to confirm it is a real calendar date
Open the full Regex Cheat Sheet & Common Patterns

Frequently asked questions

What is the regex for a YYYY-MM-DD date?

Use ^\d{4}-\d{2}-\d{2}$: four digits for the year, a hyphen, two digits for the month, a hyphen, and two digits for the day. It matches 2026-07-04 and rejects formats like 2026-7-4 (not zero-padded) or 07/04/2026 (wrong separator). Note that it is a shape check — it does not verify the month is 1–12 or the day exists in that month.

Why does my date regex accept 2026-13-40?

Because ^\d{4}-\d{2}-\d{2}$ only checks that you have four digits, two digits and two digits in the right pattern — it has no concept of a calendar. Regex is poorly suited to range-checking numbers (month 1–12, day 1–28/29/30/31 depending on the month and leap year). The right approach is to validate the format with the regex, then parse the string with a date library or the Date object and confirm it represents a real date before trusting it.

Related Regex Cheat Sheet & Common Patterns pages