Date Validation Regular Expression
A properly formatted date should follow the ISO 8601 standard (YYYY-MM-DD) for consistency and international compatibility. This format is widely supported by programming languages and databases, making it ideal for data interchange and storage. For complete datetime validation, you may also need time validation.
Recommended Solution
This regular expression validates dates in ISO 8601 format (YYYY-MM-DD) with years from 1900-2099. It performs basic structural validation, but additional logic is recommended to verify day-month combinations (e.g., February 30th, leap years).
Explanation
^- Start of the string.(?:19|20)\d{2}- Year validation. Matches years from 1900-2099 (19XX or 20XX followed by two digits).-- Hyphen separator (required by ISO 8601).(0[1-9]|1[0-2])- Month validation. Matches 01-09 or 10-12.-- Hyphen separator.(0[1-9]|[12]\d|3[01])- Day validation. Matches 01-09, 10-29, or 30-31.$- End of the string.
Note: This regex validates the date format but doesn't enforce calendar rules (e.g., February having 28/29 days, or April having 30 days). For production use, combine this regex with additional validation using your language's date parsing libraries as shown in the implementation examples below.
Implementation
The implementations below include both regex validation and additional date logic to ensure calendar correctness (leap years, days per month, etc.).
Test Cases
These test cases include edge cases such as leap years, invalid day-month combinations, and boundary conditions. The validation includes both regex pattern matching and semantic date correctness.
| Date String | Valid |
|---|---|
| 2024-01-15 | |
| 2024-12-31 | |
| 2000-02-29 | |
| 2024-02-29 | |
| 1999-06-30 | |
| 2023-07-04 | |
| 2020-11-01 | |
| 2024-02-30 | |
| 2023-02-29 | |
| 2024-04-31 | |
| 2024-06-31 | |
| 2024-09-31 | |
| 2024-11-31 | |
| 2024-13-01 | |
| 2024-00-15 | |
| 2024-01-00 | |
| 2024-01-32 | |
| 1899-12-31 | |
| 2100-01-01 | |
| 24-01-15 | |
| 2024-1-15 | |
| 2024-01-5 | |
| 2024/01/15 | |
| 15-01-2024 | |
| 01-15-2024 | |
| (empty string) | |
| not-a-date |
Basic Validation
For a quick format check without strict ISO 8601 enforcement, you can use a basic regular expression that accepts various date separators (hyphen, slash, or dot). This is useful when you need flexibility in input format but still want a YYYY-MM-DD structure.
Explanation
^- Start of the string.\d{4}- Exactly four digits for the year.[-/.]- Separator character (hyphen, slash, or dot).\d{2}- Exactly two digits for the month.[-/.]- Separator character.\d{2}- Exactly two digits for the day.$- End of the string.
Warning: This basic pattern only validates the format structure. It does not validate whether the date is semantically correct (e.g., it would accept 9999-99-99). Always combine with additional validation for production use.
Implementation
Test Cases
| Date String | Valid |
|---|---|
| 2024-01-15 | |
| 2024/01/15 | |
| 2024.01.15 | |
| 2024-12-31 | |
| 2000-02-29 | |
| 2024-02-30 | |
| 2024-13-01 | |
| 9999-99-99 | |
| 2024-1-15 | |
| 24-01-15 | |
| 2024-01-5 | |
| 15-01-2024 | |
| (empty string) | |
| not-a-date |