Date Validation Regex: YYYY-MM-DD Format
• CronOS Team
regexdatevalidationtutorialiso-8601
Need to generate a regex pattern?
Use CronOS to generate any regex pattern you wish with natural language. Simply describe what you need, and we'll create the perfect regex pattern for you. It's completely free!
Date Validation Regex: YYYY-MM-DD Format
Validate dates in ISO 8601 date format (YYYY-MM-DD) with proper month and day range validation using regex.
Pattern Breakdown
regex
^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$
Components
| Component | Description | Matches |
|---|---|---|
^ | Start anchor | Ensures match from string start |
\d{4} | Year | Exactly 4 digits |
- | Separator | Literal hyphen |
(0[1-9]|1[0-2]) | Month | 01-12 (zero-padded) |
- | Separator | Literal hyphen |
(0[1-9]|[12]\d|3[01]) | Day | 01-31 (zero-padded) |
$ | End anchor | Ensures match to string end |
Detailed Breakdown
\d{4}- Year: exactly 4 digits (0000-9999)(0[1-9]|1[0-2])- Month alternation:0[1-9]- Months 01-091[0-2]- Months 10-12
(0[1-9]|[12]\d|3[01])- Day alternation:0[1-9]- Days 01-09[12]\d- Days 10-293[01]- Days 30-31
Examples
Valid:
2024-03-152024-01-012024-12-312024-02-29(format valid, but may not be valid date)0000-01-01(format valid)
Invalid:
24-03-15(year too short)2024-3-15(month not zero-padded)2024-03-5(day not zero-padded)2024-13-15(invalid month)2024-03-32(invalid day)2024/03/15(wrong separator)03-15-2024(wrong order)
Implementation
JavaScript
javascript
const dateRegex = /^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$/;
dateRegex.test('2024-03-15'); // true
dateRegex.test('2024-12-31'); // true
dateRegex.test('2024-13-15'); // false (invalid month)
dateRegex.test('2024-03-32'); // false (invalid day)
Python
python
import re
date_regex = r'^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$'
bool(re.match(date_regex, '2024-03-15')) # True
bool(re.match(date_regex, '2024-12-31')) # True
bool(re.match(date_regex, '2024-13-15')) # False (invalid month)
Go
go
dateRegex := regexp.MustCompile(`^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$`)
dateRegex.MatchString("2024-03-15") // true
dateRegex.MatchString("2024-12-31") // true
dateRegex.MatchString("2024-13-15") // false (invalid month)
Limitations
- No leap year validation: Accepts
2024-02-30(format valid, date invalid) - No month-specific day validation: Accepts
2024-04-31(April has 30 days) - No year range validation: Accepts years like
0000or9999 - Format only: Validates format, not actual date validity
- No time component: Date only, no time
When to Use
- ISO 8601 date format validation
- Database date input (YYYY-MM-DD)
- API date parameters
- Date picker input validation
- When you need zero-padded dates
For production, consider:
- Additional validation for actual date validity (leap years, month lengths)
- Using date parsing libraries to verify dates
- Validating year ranges for your use case
- Combining with date object validation
Need to generate a regex pattern?
Use CronOS to generate any regex pattern you wish with natural language. Simply describe what you need, and we'll create the perfect regex pattern for you. It's completely free!