Regex for Date (MM/DD/YYYY) — Pattern Explained with Examples
This regex validates dates in the US MM/DD/YYYY format. It checks that the month is 01–12, the day is 01–31, and the year is four digits, all separated by forward slashes. This pattern is commonly used in North American web forms, document processing, and data exchange systems.
The Pattern
/^(0[1-9]|1[0-2])\/(0[1-9]|[12]\d|3[01])\/\d{4}$/Pattern Breakdown
| Part | Meaning |
|---|---|
^ | Start of string |
(0[1-9]|1[0-2]) | Month: 01–09 or 10–12 |
\/ | Escaped forward slash separator |
(0[1-9]|[12]\d|3[01]) | Day: 01–09, 10–29, or 30–31 |
\/ | Escaped forward slash separator |
\d{4} | Exactly four digits (year) |
$ | End of string |
Matches
01/15/202412/31/199906/01/202302/28/200007/04/2024
Does NOT Match
13/01/2024— month 13 is invalid01/32/2024— day 32 is invalid1/1/2024— missing leading zeros2024/01/15— wrong order (YYYY/MM/DD)01-15-2024— hyphens instead of slashes00/01/2024— month 00 is invalid01/01/24— year is only 2 digits
Language Examples
JavaScript
const dateRegex = /^(0[1-9]|1[0-2])\/(0[1-9]|[12]\d|3[01])\/\d{4}$/;
console.log(dateRegex.test('01/15/2024')); // true
console.log(dateRegex.test('13/01/2024')); // false
console.log(dateRegex.test('01/32/2024')); // false
// Extract month, day, year
const match = '12/25/2023'.match(dateRegex);
if (match) {
console.log(`Month: ${match[1]}, Day: ${match[2]}, Year: ${match[3]}`);
}Python
import re
date_regex = r'^(0[1-9]|1[0-2])\/(0[1-9]|[12]\d|3[01])\/\d{4}$'
print(bool(re.match(date_regex, '01/15/2024'))) # True
print(bool(re.match(date_regex, '13/01/2024'))) # False
# Extract parts
match = re.match(date_regex, '07/04/2024')
if match:
month, day, year = match.groups()
print(f'{month}/{day}/{year}')Common Pitfalls
Slash escaping in regex — The
/character must be escaped as\/inside a regex literal. Alternatively, use a different delimiter or construct the regex withnew RegExp().DD/MM vs MM/DD confusion — Users from outside the US often expect DD/MM/YYYY. Make sure your user interface clarifies the expected format.
February 30 passes — Like the YYYY-MM-DD pattern, this regex does not validate calendar logic.
02/30/2024passes but is not a real date.Leading zeros matter —
1/1/2024fails because single-digit months and days are not matched. Decide whether to accept them with an alternative pattern.
Real-World Use Cases
- US-based web forms — Standard date input for addresses, billing, and shipping information
- Document processing — Extracting dates from US government or business documents
- Data migration — Converting legacy MM/DD/YYYY data into ISO 8601 for database storage
FAQ
Related Patterns
- Regex for Date (YYYY-MM-DD)
- Regex for ISO 8601 Date/Time
- Regex for Time (HH:MM)
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro