Blog
Match dates in different formats — ISO, US, EU, and more
Dates appear in many formats and a single regex never covers them all. Here's how to match each common format separately.
ISO 8601: YYYY-MM-DD
/^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$/
Validates the year, month (01-12), and day (01-31) ranges. Does not validate semantically — Feb 30 will match. Pair with a date parser to catch that.
US: MM/DD/YYYY
/^(0[1-9]|1[0-2])\/(0[1-9]|[12]\d|3[01])\/\d{4}$/
EU/Indian: DD/MM/YYYY
/^(0[1-9]|[12]\d|3[01])\/(0[1-9]|1[0-2])\/\d{4}$/
Detecting which format you have
A string like 05/06/2024 is ambiguous — could be May 6 or June 5. Without context you can't tell. Strategies:
- Check the data source. US CSVs default to MM/DD; European or Indian to DD/MM. Document and trust the source.
- Look at the other dates. If any date has the first part > 12 (like
13/05/2024), it must be DD/MM. If the second part is > 12, it's MM/DD. - Use locale. Parse with
Intl.DateTimeFormat(JS) ordateparser(Python) and let the locale decide.
Time with date
// ISO datetime
/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?(Z|[+-]\d{2}:?\d{2})?$/
Matches 2024-06-15T10:30:00Z, 2024-06-15T10:30:00.123-05:00, etc.
Natural-language dates
For "Jun 15, 2024" or "15 June 2024", regex is the wrong tool. Use a library:
- Python:
dateparser - JavaScript:
chrono-node - Go:
github.com/araddon/dateparse
Regex matches structure; these libraries understand semantics.