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

TokenDescription
^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
\dMatches any digit (0-9)
|Alternation — matches the expression before OR after the pipe
2Matches 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
\dMatches any digit (0-9)
(?:Start of non-capturing group
:Matches the literal character ':'
[0-5]Character class — matches any one of: 0-5
\dMatches 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

InputExpected
14:30Match
23:59:59Match
24:00No Match
12:60No Match
00:00:00Match

Try It — Interactive Tester

//
gimsuy
No matches found.
Pattern: 40 charsFlags: noneMatches: 0

Ctrl+Shift+C to copy regex

Customize this pattern →