Regex for Date Format Validation
Test regex patterns for date format validation. Match YYYY-MM-DD, MM/DD/YYYY, and other date formats.
Open Regex Tester →Date Regex Patterns
The ISO 8601 date pattern
\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01]) matches YYYY-MM-DD format with basic month (01-12) and day (01-31) validation. For US format: (?:0[1-9]|1[0-2])\/(?:0[1-9]|[12]\d|3[01])\/\d{4}. Note that regex can validate format but not calendar correctness — it won't catch February 31st or leap year issues.Date Validation Best Practices
- Prefer ISO 8601: YYYY-MM-DD is unambiguous and sorts lexicographically. Use it for APIs and data storage.
- Regex for format only: Check the pattern, then parse with a date library to validate the actual date (e.g., reject Feb 30).
- Use date libraries: JavaScript's
Date, Python'sdatetime, or libraries like Day.js and date-fns handle parsing and validation correctly. - Consider locales: MM/DD/YYYY (US) vs DD/MM/YYYY (Europe) — always be explicit about which format you expect.
// ISO 8601 (YYYY-MM-DD)
/\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])/
// US format (MM/DD/YYYY)
/(?:0[1-9]|1[0-2])\/(?:0[1-9]|[12]\d|3[01])\/\d{4}/
// EU format (DD.MM.YYYY)
/(?:0[1-9]|[12]\d|3[01])\.(?:0[1-9]|1[0-2])\.\d{4}/
// JavaScript - validate with Date
const d = new Date('2024-02-29');
const isValid = !isNaN(d.getTime());