What Is a Good Regex for YYYY-MM-DD Date Validation?
Pattern: ^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$. The ISO 8601 date format YYYY-MM-DD is the international standard. This regex validates the format and constrains months to 01-12 and days to 01-31, but cannot catch semantically invalid dates like February 30. Always combine regex format checks with date parsing.
Breaking Down the Pattern
| Part | Meaning |
|---|---|
^ | Start of string |
\d{4} | Four-digit year (0000-9999) |
- | Literal hyphen separator |
(0[1-9]|1[0-2]) | Month: 01-12 |
- | Literal hyphen separator |
(0[1-9]|[12]\d|3[01])$ | Day: 01-31, end of string |
Test Cases
| Input | Match? | Note |
|---|---|---|
2026-04-11 | Yes | Valid format |
2000-01-01 | Yes | Valid format |
1999-12-31 | Yes | Valid format |
2026-13-01 | No | Month 13 does not exist |
2026-04-32 | No | Day 32 does not exist |
Usage Examples
JavaScript
const pattern = /^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$/;
pattern.test('2026-04-11'); // true
pattern.test('2026-13-01'); // false
Python
import re
pattern = r'^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$'
bool(re.match(pattern, '2026-04-11')) # True
bool(re.match(pattern, '2026-13-01')) # False
Common Pitfalls
- This regex accepts Feb 30 or Feb 31. True date validation requires calendar logic, not just regex.
- It does not validate leap years -- Feb 29 is accepted for all years.
- Use regex for format validation, then parse with Date or a library for semantic validation.
Try It Yourself
Test this regex with our Regex Tester.
Frequently Asked Questions
Can regex validate leap years?
Technically yes, but the regex becomes extremely long and unmaintainable. It is far better to validate the format with regex and then use a date library to check if the date actually exists.
Why use YYYY-MM-DD format?
ISO 8601 (YYYY-MM-DD) is unambiguous, sorts lexicographically, and is the standard in databases, APIs, and international communication. Unlike MM/DD/YYYY or DD/MM/YYYY, it cannot be misinterpreted.
Does this regex handle time zones?
No. This pattern only validates the date portion. For datetime with timezone (e.g., 2026-04-11T10:30:00Z), you need an extended pattern.