Pattern library / Dates & Time
Regex to Match Time in HH:MM (or HH:MM:SS) Format
A clock time is four digits and a colon — until 99:99 matches too. This pattern limits hours to 00–23 and minutes/seconds to 00–59, with seconds optional, in one line that works identically in JavaScript, Python, SQL and RE2 systems.
How the pattern works
([01][0-9]|2[0-3])— hours clamped to 00–23 (so 19:05 and 07:5 both handled: 07:50 style zero padding required).[0-5][0-9]— minutes and seconds clamped to 00–59.- Seconds are optional: the
(:[0-5][0-9])?group lets one pattern accept both 14:30 and 14:30:00. - Need 12-hour clock with AM/PM? That is a different shape —
^(1[0-2]|0?[1-9]):[0-5][0-9] ?([AaPp][Mm])$covers 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 | |
|---|---|---|---|
14:30 | Match | valid 24h time | Try → |
00:00 | Match | midnight | Try → |
23:59:59 | Match | with seconds | Try → |
24:00 | No match | hour max 23 | Try → |
14:60 | No match | minute max 59 | Try → |
2:30 PM | No match | 12-hour AM/PM not covered | Try → |
Where to use it
JavaScript
const re = /^([01][0-9]|2[0-3]):[0-5][0-9](:[0-5][0-9])?$/;
re.test('23:59:59'); // true
Python
import re bool(re.fullmatch(r'([01][0-9]|2[0-3]):[0-5][0-9](:[0-5][0-9])?', '14:30'))
SQL (Postgres / BigQuery)
SELECT REGEXP_MATCHES('14:30', '^([01][0-9]|2[0-3]):[0-5][0-9](:[0-5][0-9])?$');
Common mistakes
- Single-digit hours like 2:30 do not match — leading zero is required by the 00-23 shape. Loosen to ([01]?[0-9]|2[0-3]) if your data is not zero-padded.
- Durations like 01:30:45 (over 24h) will not match the hour clamp — use [0-9]+ for duration patterns instead.
- AM/PM formats need the separate 12-hour pattern from the notes.
FAQ
How do I match both HH:MM and HH:MM:SS with one regex?
Make the seconds group optional: ^([01][0-9]|2[0-3]):[0-5][0-9](:[0-5][0-9])?$. One pattern matches 14:30 and 14:30:00.
Why does 24:00 not match?
The hour branch is ([01][0-9]|2[0-3]), so the maximum is 23. ISO 8601 allows 24:00 as an edge case for end-of-day; add it as an alternative — 24:00 — if your data uses it.
Can I use this in GA4?
Yes, it is plain RE2 syntax. Wrap it for partial matching (.* around it) if your GA4 field contains more than the time itself.
Related patterns
ISO 8601 DateTime Regex — Match & Test Online · Regex to Match Date (YYYY-MM-DD) — Test & Copy · 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.