Skip to main content
hank-builds.com

Time (HH:MM) regex

Matches 24-hour time format.

/(?:[01]\d|2[0-3]):[0-5]\d/g

What it matches

A 24-hour clock time: hours 00-23 through the [01]\d | 2[0-3] alternation, a colon, then minutes 00-59. Both fields must be zero padded, which is what ISO 8601 requires and what log formats and an <input type="time"> value produce.

Railroad diagram

Read left to right. Every path through the diagram is a string the pattern accepts.

[01] \d "2" [0-3] ":" [0-5] \d

Drag to pan. Ctrl+scroll to zoom.

Breakdown

gFind all matches instead of stopping after the first
[01]Match one of: Literal "0", Literal "1"
\dDigit (0-9)
2Literal "2"
[0-3]Match one of: Literal "0"–Literal "3"
:Literal ":"
[0-5]Match one of: Literal "0"–Literal "5"
\dDigit (0-9)

Test cases

Matches

  • 09:30
  • 12:00
  • 23:59

Does not match

  • 24:00
  • 9:30
  • 12:60

Caveats

No seconds, no timezone offset, no 12-hour am/pm suffix, and unpadded times like 9:30 do not match. Because it is unanchored it finds a time inside a longer string, which is what you want when scanning logs but not when validating a field, so anchor it with ^ and $ for that.

More dates & times patterns

Test this against your own input in the regex tester, browse the full pattern library, or check the syntax cheat sheet.