Regex to Match YYYY-MM-DD Dates

Validate dates in YYYY-MM-DD (ISO 8601) format with this regex pattern. Checks valid month (01-12) and day (01-31) ranges. Free regex tester.

Regular Expression

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

Token Breakdown

TokenDescription
^Anchors at the start of the string (or line in multiline mode)
\dMatches any digit (0-9)
{4}Matches exactly 4 times
-Matches the literal character '-'
(?:Start of non-capturing group
0Matches the literal character '0'
[1-9]Character class — matches any one of: 1-9
|Alternation — matches the expression before OR after the pipe
1Matches the literal character '1'
[0-2]Character class — matches any one of: 0-2
)End of group
-Matches the literal character '-'
(?:Start of non-capturing group
0Matches the literal character '0'
[1-9]Character class — matches any one of: 1-9
|Alternation — matches the expression before OR after the pipe
[12]Character class — matches any one of: 12
\dMatches any digit (0-9)
|Alternation — matches the expression before OR after the pipe
3Matches the literal character '3'
[01]Character class — matches any one of: 01
)End of group
$Anchors at the end of the string (or line in multiline mode)

Detailed Explanation

This regex validates dates in the YYYY-MM-DD format, which is the ISO 8601 standard. Here is the token-by-token breakdown:

^ — Anchors the match at the start of the string.

\d{4} — Matches exactly four digits for the year. This accepts any four-digit year from 0000 to 9999.

  • — Matches a literal hyphen separating the year from the month.

(?:0[1-9]|1[0-2]) — A non-capturing group with two alternatives for valid months (01-12). 0[1-9] matches months 01-09, and 1[0-2] matches months 10-12. This prevents invalid months like 00 or 13.

  • — Matches a literal hyphen separating the month from the day.

(?:0[1-9]|[12]\d|3[01]) — A non-capturing group with three alternatives for valid days (01-31). 0[1-9] matches days 01-09, [12]\d matches days 10-29, and 3[01] matches days 30-31. This prevents invalid days like 00 or 32.

$ — Anchors the match at the end of the string.

This pattern validates the format and basic ranges but does not account for month-specific day limits. For example, it would accept 2024-02-31 even though February never has 31 days. For full calendar validation, combine this regex with programmatic checks for month lengths and leap years. The ISO 8601 date format is widely used in databases, APIs, and international data exchange because it is unambiguous and sorts chronologically.

Example Test Strings

InputExpected
2024-01-15Match
2023-12-31Match
2024-13-01No Match
2024-00-15No Match
2024-06-30Match

Try It — Interactive Tester

//
gimsuy
No matches found.
Pattern: 49 charsFlags: noneMatches: 0

Ctrl+Shift+C to copy regex

Customize this pattern →