Regex Pattern for Date Formats

Date format validation is a two-step problem that developers often try to solve with a single tool. Regex is excellent for format validation — ensuring a date string follows a specific structure — but is fundamentally incapable of semantic validation, which involves understanding the Gregorian calendar, leap years, and month lengths. A combined approach works best: regex checks that the string looks like a date, then a date library verifies it's a real calendar date. The ISO 8601 pattern in this example validates YYYY-MM-DD format specifically. The year segment \d{4} requires exactly four digits. The month segment (0[1-9]|1[0-2]) allows only 01 through 12 — it correctly rejects 00 and 13 using the same numeric range technique as IP address validation. The day segment (0[1-9]|[12]\d|3[01]) allows 01–31, which is correct for format validation even though not all months have 31 days. The test cases highlight the key scenarios: a valid mid-year date, a valid end-of-year date, an invalid month 13, an invalid month 00, a clearly non-date string, a date with single-digit month and day that fails the zero-padding requirement, and a date written in YYYYMMDD format without separators. Common date format pitfalls: US format (MM/DD/YYYY) and European format (DD/MM/YYYY) are ambiguous for dates like 04/05/2024 — is that April 5th or May 4th? ISO 8601 eliminates this ambiguity with its YYYY-MM-DD ordering, which is why it's the standard for APIs, databases, filenames, and log files. Always prefer ISO 8601 in any system that crosses locale boundaries. Validation across multiple formats: if your application accepts dates from international users, you'll need to either enforce a single format (ISO 8601 is recommended) or use alternation to handle multiple formats: /^(\d{4}-\d{2}-\d{2}|\d{2}\/\d{2}\/\d{4})$/ matches either ISO or US format. Parse the matched string with a date library afterward to validate it's a real date and normalize it to ISO 8601 for storage. Real-world scenarios: filtering form input before passing to Date.parse() to avoid NaN results; validating date columns in a CSV upload; ensuring log file date components are in the correct format before archiving. Tips: date-fns, Temporal (the new JavaScript date API), and Luxon all provide isValid() methods that correctly handle leap years and month lengths after you've confirmed the format with regex.

Example
/^d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]d|3[01])$/

# ISO 8601 test cases:
2024-01-15
2024-12-31
2024-13-01
2024-00-15
not-a-date
2024-1-5
20240115
[ open in Regex Tester → ]

FAQ

Can regex validate dates like February 30?
No. Regex checks format and ranges for each component independently but cannot verify that a date is a real calendar day. Use a date library like date-fns or Temporal for semantic validation.
How do I match multiple date formats with one regex?
Combine patterns with alternation using the | operator, wrapping each format in a non-capturing group (?:...) so the alternation applies to the entire format.
What is the ISO 8601 date format?
ISO 8601 uses YYYY-MM-DD ordering, which is unambiguous regardless of locale. It is the recommended format for APIs, databases, and file names.

Related Examples