Regex Date & Time Parsing

Generate regex patterns for common date and time formats. Pick a format — get a regex with named groups, a live tester, and language-specific code.

 

How to Use This Tool

Select a date format from the chip selector. The tool generates a regex pattern with named capture groups for extracting date components (year, month, day, etc.). Toggle "Strict" mode for anchored patterns (^...$) that validate entire strings, or uncheck it for extraction mode that finds dates embedded in text.

Paste a sample string into the live tester to see matches highlighted in real time. The language integration section shows how to use the regex in JavaScript, Python, and Go with proper named group syntax.

Common Date Regex Patterns

FormatPatternMatches
ISO date\d{4}-\d{2}-\d{2}2026-03-26
ISO datetime\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}2026-03-26T14:30:00
US date\d{1,2}/\d{1,2}/\d{4}3/26/2026, 03/26/2026
EU date\d{1,2}/\d{1,2}/\d{4}26/3/2026, 26/03/2026
12-hour time\d{1,2}:\d{2}\s?[AaPp][Mm]2:30 PM, 2:30PM
24-hour time([01]?\d|2[0-3]):\d{2}(:\d{2})?14:30, 14:30:00
Unix epoch (s)\d{10}1774533600
Unix epoch (ms)\d{13}1774533600000
Flexible separator\d{4}[-/.]\d{1,2}[-/.]\d{1,2}2026-03-26, 2026/3/26

Named Group Syntax by Language

LanguageNamed group syntaxAccess match
JavaScript(?<year>\d{4})match.groups.year
Python(?P<year>\d{4})match.group('year')
Go(?P<year>\d{4})match[re.SubexpIndex("year")]
Java(?<year>\d{4})matcher.group("year")
C# / .NET(?<year>\d{4})match.Groups["year"].Value
PHP(?P<year>\d{4})$match['year']
Ruby(?<year>\d{4})match[:year]
Rust(?P<year>\d{4})caps.name("year")

Important Caveats

Regex is great for extracting date-like strings from text. It is not great for validating that a string represents a real date. Keep these rules in mind:

  • Regex cannot validate calendar logic. The pattern \d{2}-\d{2} happily matches 13-45, which isn't a real month-day. Always validate extracted components with your language's date library.
  • Ambiguity is unavoidable. 01/02/2026 is January 2 (US) or February 1 (EU). Regex can't resolve this — you need locale context.
  • Leap years require math, not regex. February 29 is valid in 2024 but not 2025. Don't try to encode leap year rules in a regex pattern.
  • Use strict mode for validation (anchored with ^...$). Use extraction mode (unanchored) for finding dates in larger text.
  • For complex parsing, use a library. chrono-node (JavaScript), dateutil (Python), or time.Parse (Go) handle edge cases that regex never will.

Frequently Asked Questions

How do I match a date with regex?

For ISO (YYYY-MM-DD): \d{4}-\d{2}-\d{2}. For US (MM/DD/YYYY): \d{1,2}/\d{1,2}/\d{4}. Use named groups for extraction: (?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2}). Always validate matched values with a date library — regex can match invalid dates like 2026-13-45.

Can regex validate that a date is real?

No. Regex can match the format but not calendar logic. It will happily match February 30 or month 13. Best practice: use regex to extract components, then validate with new Date() (JS), datetime.strptime() (Python), or time.Parse() (Go).

How do I match both MM/DD/YYYY and DD/MM/YYYY?

The pattern is identical: \d{1,2}/\d{1,2}/\d{4}. The ambiguity is semantic, not syntactic — regex can't tell whether the first group is a month or day. You need locale context or a user-specified format to disambiguate.

What regex matches ISO 8601 with optional timezone?

\d{4}-\d{2}-\d{2}(T\d{2}:\d{2}(:\d{2})?(\.\d+)?(Z|[+-]\d{2}:?\d{2})?)?. This matches bare dates, datetime, with or without seconds, fractional seconds, Z suffix, and numeric UTC offsets.

How do I extract dates from unstructured text?

Use an unanchored regex with the global flag: /\d{4}-\d{2}-\d{2}/g (JS) or re.findall(r'\d{4}-\d{2}-\d{2}', text) (Python). For natural language dates ("next Tuesday"), use a library like chrono-node or dateutil instead of regex.

How do named capture groups work?

Named groups let you label each part of the match. In JavaScript: /(?<year>\d{4})-(?<month>\d{2})/ gives match.groups.year. Python uses (?P<year>...) syntax. They make code self-documenting and resilient to pattern changes — much better than positional indexes.

Related Tools