Regex for Date (YYYY-MM-DD)
This regex matches dates in the ISO 8601 format YYYY-MM-DD. It validates that months are between 01-12 and days are between 01-31. The ISO 8601 date format is the international standard and is the recommended format for data interchange, APIs, and database storage. Note that this pattern does not validate logical date correctness (e.g., February 30).
^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$ What is the regex pattern for Date (YYYY-MM-DD)?
The regex pattern for Date (YYYY-MM-DD) is ^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$. This regex matches dates in the ISO 8601 format YYYY-MM-DD. It validates that months are between 01-12 and days are between 01-31. The ISO 8601 date format is the international standard and is the recommended format for data interchange, APIs, and database storage. Note that this pattern does not validate logical date correctness (e.g., February 30). This pattern is commonly used for api date validation and database date fields.
Test Examples
2024-01-15 2024-01-15 2023-12-31 2023-12-31 2024-13-01 Common Uses
- ✓ API date validation
- ✓ Database date fields
- ✓ Form date inputs
- ✓ Log file date parsing
Variations
With slashes
^\d{4}/(0[1-9]|1[0-2])/(0[1-9]|[12]\d|3[01])$ Uses forward slashes as separator
Flexible separator
^\d{4}[\-/](0[1-9]|1[0-2])[\-/](0[1-9]|[12]\d|3[01])$ Accepts dash or slash separators
Optional leading zeros
^\d{4}-(0?[1-9]|1[0-2])-(0?[1-9]|[12]\d|3[01])$ Allows single-digit months and days
Frequently Asked Questions
Does this validate dates like February 30?
No. This regex only checks the format. February 30, April 31, and other logically invalid dates will still match. You need programmatic validation to check calendar correctness.
Why use YYYY-MM-DD format?
ISO 8601 (YYYY-MM-DD) is the international standard for date representation. It sorts correctly as a string, is unambiguous across locales, and is the default format for most databases and APIs.
Can I use this for date range validation?
This regex only validates format, not ranges. To check if a date falls within a range, parse it into a Date object first, then compare programmatically.