Pattern library / Dates & Time
Regex to Match an ISO 8601 DateTime
Log lines, API payloads and analytics exports all stamp events as ISO 8601: 2026-10-28T14:30:00Z. This pattern accepts the practical forms — date, T or space separator, seconds optional, fractional seconds, and a trailing Z or numeric offset — while staying RE2-compatible.
^[0-9]{4}-[0-9]{2}-[0-9]{2}[T ][0-9]{2}:[0-9]{2}(:[0-9]{2}(\.[0-9]+)?)?(Z|[+-][0-9]{2}:?[0-9]{2})?$
Load this pattern into the tester →
How the pattern works
[T ]accepts both the officialTand the space separator many CSV exports use.- Seconds and fractional seconds are optional because APIs return both
14:30and14:30:00.482Z. - Hours above 23 still pass (
25:00matches). Add([01][0-9]|2[0-3])for hour if you need the clamp — trade readability for strictness. - Leap-second
:60is not covered; real systems almost never emit it.
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-28T14:30:00Z | Match | full form with Z | Try → |
2026-10-28 14:30 | Match | space separator, no seconds | Try → |
2026-10-28T14:30:00.123+07:00 | Match | fraction + offset | Try → |
2026-10-28T14:30 | Match | T separator, minutes only | Try → |
28/10/2026 14:30 | No match | wrong date shape | Try → |
2026-10-28T25:00:00Z | No match | hour must be 00-23 | Try → |
Where to use it
JavaScript
const re = /^[0-9]{4}-[0-9]{2}-[0-9]{2}[T ][0-9]{2}:[0-9]{2}(:[0-9]{2}(\.[0-9]+)?)?(Z|[+-][0-9]{2}:?[0-9]{2})?$/;
re.test('2026-10-28T14:30:00Z'); // true
Bash / grep (logs)
grep -E '^[0-9]{4}-[0-9]{2}-[0-9]{2}[T ][0-9]{2}:[0-9]{2}(:[0-9]{2}(\.[0-9]+)?)?(Z|[+-][0-9]{2}:?[0-9]{2})?' app.log
BigQuery (RE2)
SELECT ts FROM logs
WHERE REGEXP_CONTAINS(ts, r'^[0-9]{4}-[0-9]{2}-[0-9]{2}[T ][0-9]{2}:[0-9]{2}(:[0-9]{2}(\.[0-9]+)?)?(Z|[+-][0-9]{2}:?[0-9]{2})?$')
Common mistakes
- Do not use \d inside a character-class-heavy pattern inconsistently — both work everywhere, but sticking to [0-9] keeps the pattern portable to tools where \d matches non-ASCII digits.
- Timezone offsets like +0700 (no colon) are covered; offsets with seconds are not — they are vanishingly rare.
- To match only UTC-stamped values, keep the trailing (Z) group and drop the offset alternative.
FAQ
What date-time formats does this pattern match?
ISO 8601 basics: YYYY-MM-DD, T or space, HH:MM with optional seconds, optional fractional seconds, and an optional trailing Z or +hh:mm / -hh:mm offset.
Why does my pattern fail on 2026-10-28T14:30:00.123456Z?
It should not — the fractional part is (\.[0-9]+), which accepts any number of digits. Paste it in the tester to see which segment fails.
Does this work in GA4 and BigQuery?
Yes — the pattern is pure character classes, groups and alternation, all valid RE2 syntax used by BigQuery REGEXP_CONTAINS and GA4.
Related patterns
Regex to Match Date (YYYY-MM-DD) — Test & Copy · Time Regex (HH:MM / HH:MM:SS) — 24-Hour Match · Log Level Regex — Match INFO / WARN / ERROR Lines
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.