DevKits

Dates & Times

Match a 24-hour time

Match times like 09:30 or 23:59:59 in 24-hour format.

Pattern

Regular expression

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

How it works

Hours 00–23, minutes 00–59, seconds optional but if present also 00–59. Handles both `HH:MM` and `HH:MM:SS` shapes.

Matches

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

Non-matches

  • 24:00 (invalid hour)
  • 09:60 (invalid minute)
  • 9:30 (missing leading zero)

Language snippets

JavaScript

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

Python

re.findall(r"(?:[01]\d|2[0-3]):[0-5]\d(?::[0-5]\d)?", text)

Go

re := regexp.MustCompile(`(?:[01]\d|2[0-3]):[0-5]\d(?::[0-5]\d)?`)

Java

Pattern.compile("(?:[01]\\d|2[0-3]):[0-5]\\d(?::[0-5]\\d)?")

Frequently asked questions

What is the regex for 24-hour time?

The pattern (?:[01]\d|2[0-3]):[0-5]\d(?::[0-5]\d)? matches 24-hour time. Hours 00–23, minutes 00–59, seconds optional but if present also 00–59. Handles both `HH:MM` and `HH:MM:SS` shapes.

What does this 24-hour time pattern match?

It matches strings like 00:00, 09:30, 23:59:59, but rejects 24:00 (invalid hour), 09:60 (invalid minute).

How do I use this pattern in JavaScript, Python, Go, Java?

Ready-to-run snippets are provided for JavaScript, Python, Go, Java. Copy the one for your language from the Language snippets section — each wraps the same core pattern in that language's regex API.

Related patterns

Try this pattern in a tool

Try any pattern live in the Regex Tester