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
| Token | Description |
|---|---|
| ^ | Anchors at the start of the string (or line in multiline mode) |
| \d | Matches any digit (0-9) |
| {4} | Matches exactly 4 times |
| - | Matches the literal character '-' |
| (?: | Start of non-capturing group |
| 0 | Matches the literal character '0' |
| [1-9] | Character class — matches any one of: 1-9 |
| | | Alternation — matches the expression before OR after the pipe |
| 1 | Matches 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 |
| 0 | Matches 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 |
| \d | Matches any digit (0-9) |
| | | Alternation — matches the expression before OR after the pipe |
| 3 | Matches 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
| Input | Expected |
|---|---|
| 2024-01-15 | Match |
| 2023-12-31 | Match |
| 2024-13-01 | No Match |
| 2024-00-15 | No Match |
| 2024-06-30 | Match |
Try It — Interactive Tester
49 charsFlags: noneMatches: 0Ctrl+Shift+C to copy regex
Related Regex Patterns
Regex to Match MM/DD/YYYY Dates
/^(?:0[1-9]|1[0-2])\/(?:0[1-9]|[12]\d|3[01])\/\d{4}$/
Regex to Match ISO 8601 DateTime
/^\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])T(?:[01]\d|2[0-3]):[0-5]\d:[0-5]\d(?:\.\d{1,3})?(?:Z|[+-](?:[01]\d|2[0-3]):[0-5]\d)$/
Regex to Match 12-Hour Time Format
/^(?:0?[1-9]|1[0-2]):[0-5]\d(?::[0-5]\d)?\s?[AaPp][Mm]$/
Regex to Match 24-Hour Time Format
/^(?:[01]\d|2[0-3]):[0-5]\d(?::[0-5]\d)?$/