Regex for Date (MM/DD/YYYY)
This regex matches dates in the US-style MM/DD/YYYY format with forward slash separators. It validates that months are 01-12 and days are 01-31. This format is commonly used in US-facing applications, government forms, and legacy systems. For international or API use, prefer the ISO 8601 YYYY-MM-DD format instead.
^(0[1-9]|1[0-2])/(0[1-9]|[12]\d|3[01])/\d{4}$ What is the regex pattern for Date (MM/DD/YYYY)?
The regex pattern for Date (MM/DD/YYYY) is ^(0[1-9]|1[0-2])/(0[1-9]|[12]\d|3[01])/\d{4}$. This regex matches dates in the US-style MM/DD/YYYY format with forward slash separators. It validates that months are 01-12 and days are 01-31. This format is commonly used in US-facing applications, government forms, and legacy systems. For international or API use, prefer the ISO 8601 YYYY-MM-DD format instead. This pattern is commonly used for us date form validation and legacy system integration.
Test Examples
01/15/2024 01/15/2024 12/31/2023 12/31/2023 13/01/2024 Common Uses
- ✓ US date form validation
- ✓ Legacy system integration
- ✓ Government forms
- ✓ CSV data parsing
Variations
With dashes
^(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])-\d{4}$ Uses dashes instead of slashes
Optional leading zeros
^(0?[1-9]|1[0-2])/(0?[1-9]|[12]\d|3[01])/\d{4}$ Allows 1/5/2024 instead of requiring 01/05/2024
Two-digit year
^(0[1-9]|1[0-2])/(0[1-9]|[12]\d|3[01])/\d{2}$ Matches MM/DD/YY with two-digit year
Frequently Asked Questions
Why prefer YYYY-MM-DD over MM/DD/YYYY?
YYYY-MM-DD (ISO 8601) is internationally unambiguous and sorts correctly as a string. MM/DD/YYYY is only used in the US and can be confused with DD/MM/YYYY used in Europe. Use ISO 8601 for APIs and storage.
How do I handle both MM/DD/YYYY and DD/MM/YYYY?
You cannot distinguish between these formats by regex alone for dates like 03/04/2024. You need to know the expected format from context, locale settings, or user preference.
Does this validate February 29 in leap years?
No. Regex cannot perform the arithmetic needed for leap year validation. Parse the matched date and validate it programmatically.