Back to all patterns
ISO 8601 Date Validation
Dates & Numbers
Validates dates in ISO 8601 format (YYYY-MM-DD).
/^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$/How it works
This pattern validates the YYYY-MM-DD date format. Month must be 01–12 and day must be 01–31. Note that it does not validate calendar correctness, February 31 would pass. For full date validation, parse with a date library after the regex check.
Test Cases
Should Match
- 2024-01-15
- 2000-12-31
- 1999-06-01
Should NOT Match
- 2024-13-01
- 2024-00-15
- 24-01-15
Quick Summary
Validates dates in YYYY-MM-DD (ISO 8601) format. Checks month range (01–12) and day range (01–31) but does not validate calendar correctness (e.g., Feb 30 passes). Always parse with a date library for full validation.
Key Takeaways
Key Takeaways
- Validates YYYY-MM-DD format with zero-padded month and day
- Month range: 01–12; Day range: 01–31, does not check days-per-month
- Does not validate time components, use a separate pattern for datetime strings
- After regex validation, parse with Date.parse() or a library to catch invalid calendar dates
Use Cases
When to use it
- Validating date inputs in API request bodies
- Parsing date strings from CSV or JSON data files
- Enforcing consistent date format in form inputs
Watch out
Common Mistakes
- Assuming this catches all invalid dates, Feb 31 and Apr 31 will pass
- Not accounting for leap years, use a date library for that
- Forgetting that ISO 8601 also covers datetime (2024-01-15T10:30:00Z), this pattern only covers the date part
FAQ
ISO 8601 Date Validation, Frequently Asked
How do I also validate the time component?
Extend the pattern: ^\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]\dZ$
What is the difference between ISO 8601 and RFC 3339?
RFC 3339 is a profile of ISO 8601 with stricter rules (e.g., T separator required, timezone mandatory). Most APIs use RFC 3339.