Regex for 24-hour time (HH:MM)
Time in 24-hour format with optional seconds.
The 24-hour time (HH:MM) regex is ^([01]\d|2[0-3]):[0-5]\d(:[0-5]\d)?$ — copy it below, or open it in the explainer for a token-by-token breakdown.
The pattern
^([01]\d|2[0-3]):[0-5]\d(:[0-5]\d)?$
What it matches
00:0013:4523:59:5909:3012:00:00
What it doesn't match
24:0012:609:3013:45:60
Notes & gotchas
Hours 00–23, minutes/seconds 00–59. Seconds are optional. For times with fractions, append (\.\d+)?.
Code in your language
Use the explainer's Code tab to generate ready-to-paste snippets in JavaScript, Python, Java, .NET, Go, Ruby, and PHP for this pattern.
Open in explainer →Token-by-token breakdown
Every part of the pattern, left to right:
| Token | Meaning |
|---|---|
^ | start of string (or line in multiline mode) |
( | start of a capturing group |
[01] | any of: “0”, “1” |
\d | any digit (0–9) |
| | OR — try the alternative |
2 | literal text “2” |
[0-3] | any of: 0–3 |
) | end of group |
: | literal text “:” |
[0-5] | any of: 0–5 |
\d | any digit (0–9) |
( | start of a capturing group |
: | literal text “:” |
[0-5] | any of: 0–5 |
\d | any digit (0–9) |
)? | end of group, optional (zero or one) |
$ | end of string (or line in multiline mode) |
About this pattern
Date and time formats vary enormously across regions and systems. The regex here validates structure only — a date like 2024-02-30 would pass format validation but isn't a real date. After regex passes, parse with your language's date library to confirm semantic validity.
Quick usage in different languages
This exact pattern — with the correct escaping and idioms for each language:
- JavaScript:
/^([01]\d|2[0-3]):[0-5]\d(:[0-5]\d)?$/.test(value) - Python:
re.match(r"^([01]\d|2[0-3]):[0-5]\d(:[0-5]\d)?$", value) - Java:
Pattern.compile("^([01]\\d|2[0-3]):[0-5]\\d(:[0-5]\\d)?$").matcher(value).matches() - C# / .NET:
Regex.IsMatch(value, @"^([01]\d|2[0-3]):[0-5]\d(:[0-5]\d)?$") - Go:
regexp.MustCompile(`^([01]\d|2[0-3]):[0-5]\d(:[0-5]\d)?$`).MatchString(value) - Ruby:
/^([01]\d|2[0-3]):[0-5]\d(:[0-5]\d)?$/.match?(value) - PHP:
preg_match('~^([01]\d|2[0-3]):[0-5]\d(:[0-5]\d)?$~', $value)
The explainer’s Code tab regenerates these for any pattern you paste, and the downloadable cheat sheet bundles the breakdown, all seven snippets, and the pitfalls below onto one printable page.
Common pitfalls
- Anchored to the whole string. This pattern uses ^ and $, so it requires the entire input to match. To find it inside a longer text, drop the anchors and use the global (g) flag.
- Escape it correctly per language. In Java and JavaScript strings each backslash must be doubled (\\d); in Python, Go, and C# use raw/verbatim strings so the backslashes survive.
- Validate beyond format. Matching the format doesn't guarantee the value is real. Confirm the 24-hour time (hh:mm) against a source of truth (database, API, or checksum) where it matters.
Related patterns
More patterns in the Date & time category:
- EU / Indian date (DD/MM/YYYY)
- 12-hour time (with AM/PM)
- US date (MM/DD/YYYY)
- Timezone offset
- ISO 8601 datetime
- ISO 8601 duration
See also
Browse all 300 patterns in the library, or open this regex in the interactive explainer for a token-by-token breakdown, live testing, and code in seven languages.
Want more patterns? Browse the full library →