Regex to Match 24-Hour Time Format
Validate 24-hour (military) time format with this regex pattern. Matches times from 00:00 to 23:59:59 including optional seconds. Free regex tool.
Regular Expression
/^(?:[01]\d|2[0-3]):[0-5]\d(?::[0-5]\d)?$/
Token Breakdown
| Token | Description |
|---|---|
| ^ | Anchors at the start of the string (or line in multiline mode) |
| (?: | Start of non-capturing group |
| [01] | Character class — matches any one of: 01 |
| \d | Matches any digit (0-9) |
| | | Alternation — matches the expression before OR after the pipe |
| 2 | Matches the literal character '2' |
| [0-3] | Character class — matches any one of: 0-3 |
| ) | End of group |
| : | Matches the literal character ':' |
| [0-5] | Character class — matches any one of: 0-5 |
| \d | Matches any digit (0-9) |
| (?: | Start of non-capturing group |
| : | Matches the literal character ':' |
| [0-5] | Character class — matches any one of: 0-5 |
| \d | Matches any digit (0-9) |
| ) | End of group |
| ? | Makes the preceding element optional (zero or one times) |
| $ | Anchors at the end of the string (or line in multiline mode) |
Detailed Explanation
This regex validates times in the 24-hour (military) format. Here is the token-by-token breakdown:
^ — Anchors the match at the start of the string.
(?:[01]\d|2[0-3]) — A non-capturing group matching valid hours (00-23). Two alternatives handle the ranges: [01]\d matches hours 00-19, and 2[0-3] matches hours 20-23. This prevents invalid hours like 24, 25, etc.
: — Matches a literal colon separating hours from minutes.
[0-5]\d — Matches valid minutes (00-59). The first digit is restricted to 0-5 to ensure the minutes value does not exceed 59.
(?::[0-5]\d)? — An optional non-capturing group for seconds. When present, it matches a colon followed by valid seconds (00-59). Many applications display time without seconds.
$ — Anchors the match at the end of the string.
The 24-hour time format is used internationally and is the standard in most countries outside the United States. It is also the standard format for computing, databases, and log files because it eliminates the ambiguity of AM/PM. This pattern matches times from 00:00 (midnight) to 23:59:59 (one second before midnight). Note that the special time 24:00 (used in some railway timetables to denote the end of a day) is not matched by this pattern. If you need to accept 24:00, add it as an additional alternative in the hours group.
Example Test Strings
| Input | Expected |
|---|---|
| 14:30 | Match |
| 23:59:59 | Match |
| 24:00 | No Match |
| 12:60 | No Match |
| 00:00:00 | Match |
Try It — Interactive Tester
40 charsFlags: noneMatches: 0Ctrl+Shift+C to copy regex
Related Regex Patterns
Regex to Match 12-Hour Time Format
/^(?:0?[1-9]|1[0-2]):[0-5]\d(?::[0-5]\d)?\s?[AaPp][Mm]$/
Regex to Match ISO 8601 DateTime
/^\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])T(?:[01]\d|2[0-3]):[0-5]\d:[0-5]\d(?:\.\d{1,3})?(?:Z|[+-](?:[01]\d|2[0-3]):[0-5]\d)$/
Regex to Match YYYY-MM-DD Dates
/^\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])$/
Regex to Match MM/DD/YYYY Dates
/^(?:0[1-9]|1[0-2])\/(?:0[1-9]|[12]\d|3[01])\/\d{4}$/