Pattern library / Dates & Time
Regex to Match Dates in YYYY-MM-DD Format
\d{4}-\d{2}-\d{2} looks right and mostly is — but left loose it happily matches 9999-99-99 and the middle of a sentence. This is the full-match version with sane range limits, and it is valid RE2, so it works in GA4, BigQuery, Go and Rust as well as JavaScript.
How the pattern works
^[0-9]{4}-— exactly four digits for the year, anchored at the start.(0[1-9]|1[0-2])— month limited to 01–12 instead of any two digits.(0[1-9]|[12][0-9]|3[01])— day limited to 01–31. Februari 30 still passes: regex validates shape, not the calendar. For real calendar validation, parse the value in code after matching.- Drop
^and$when you only need to find dates inside longer text.
Test it: match & no-match examples
Every row is loaded straight into the tester — click Try to see the match live:
| Input | Result | Why | |
|---|---|---|---|
2026-10-28 | Match | valid date, full match | Try → |
1999-01-31 | Match | month 01, day 31 | Try → |
2026-13-01 | No match | month 13 rejected | Try → |
2026-00-10 | No match | month 00 rejected | Try → |
2026-10-32 | No match | day 32 rejected | Try → |
order 2026-10-28 shipped | No match | anchors force full match | Try → |
Where to use it
JavaScript
const re = /^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])$/;
re.test('2026-10-28'); // true
Python
import re
re.match(r'^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])$', '2026-10-28')
BigQuery / GA4 (RE2)
SELECT REGEXP_CONTAINS('2026-10-28', r'^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])$')
Common mistakes
- Regex cannot tell 2026-02-30 from a real date. Use
datetime.strptime/new Date()for the final check. - American
MM/DD/YYYYis a different shape — do not reuse this pattern for it. - In GA4 filters the pattern full-matches by default; keep the anchors if you want exact values, remove them for partially matches regex surfaces — see match types.
FAQ
Does this regex validate real calendar dates?
It validates the YYYY-MM-DD shape and range limits (month 01-12, day 01-31). Impossible dates like 2026-02-30 still match — confirm with a date parser in your language after matching.
How do I match a date inside a longer string?
Remove the ^ and $ anchors: [0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01]) finds the date anywhere in the text.
Does this work in Google Analytics 4?
Yes. The pattern uses only character classes, groups and alternation, all supported by the RE2 engine GA4 uses. Test it in the tester first, then paste it into your GA4 filter.
Related patterns
ISO 8601 DateTime Regex — Match & Test Online · Time Regex (HH:MM / HH:MM:SS) — 24-Hour Match · Extract Numbers from String — Regex + Examples
For GA4-specific patterns, see the GA4 regex cheat sheet, the page path regex guide, the event filter regex guide, the GA4 regex match types, the custom channel grouping guide and RE2 vs PCRE. Browse the full regex pattern library.