gettoolz.app

24-hour time regex

Hours 00 to 23 and minutes 00 to 59, zero-padded, with optional seconds. Copy the pattern, with 3 inputs it accepts and 5 it rejects, each checked against it at build time.

Part of developer tools.

Pattern^(?:[01]\d|2[0-3]):[0-5]\d(?::[0-5]\d)?$
JavaScript literal/^(?:[01]\d|2[0-3]):[0-5]\d(?::[0-5]\d)?$/

What the 24-hour time regex accepts and rejects

Each result below is the output of the pattern itself, run on the input when this page was built — not a claim copied next to it. A wrong verdict stops the build.

Inputs tested against the 24-hour time regex
InputResultWhy
00:00Passesmidnight
09:30Passesa zero-padded hour
23:59:59Passesthe last second of the day
24:00Failshour 24
9:30Failshour not zero-padded
12:60Failsminute 60
12:30 PMFailsa 12-hour suffix
23:59:60Failsa leap second

Limits

  • Leap seconds (23:59:60) and the end-of-day 24:00 are both legal in ISO 8601 and both refused here, which is what most forms want.
  • Fractional seconds (12:30:15.250) and time-zone offsets are not covered.

Try it on your own input

Open this pattern in the regex tester with every example above on its own line. The link adds the g and m flags so that ^ and $ hold per line and every line is tested: the highlighted lines are the ones marked as passing here. Paste your own data in place of the examples — it is matched in your browser and never uploaded.

Other common regexes

Or open the regex tester empty and bring your own pattern.