Start free · Free tools · Fair usage limits
Browse Tools

Categories

Custom AI Solutions – AI2Flows
LIVE NEWS
Loading latest AI & SEO news…
Home›Tools›Regex Tester›Regex for time (24-hour)
Regex pattern · validation

Regex for time (24-hour)

Regex for 24-hour time: a tested pattern with examples, a plain-English breakdown, and code for JavaScript, Python, PHP, Java and Go.

Quick answer

Use /^(?:[01]\d|2[0-3]):[0-5]\d(?::[0-5]\d)?$/ as a time (24-hour) regex. It matches "00:00" and "23:59", and rejects "24:00" and "9:30". Requires a two-digit hour.

Last updated · Maintained by FreeAIToolz · How we test

/^(?:[01]\d|2[0-3]):[0-5]\d(?::[0-5]\d)?$/
  • 00:00matches
  • 23:59matches
  • 14:30:15matches
  • 24:00no match
  • 9:30no match
  • 12:60no match
  • 12:30:60no match
Open in the full Regex Tester (replace, flags, groups) →

How this regex works

PartMeaning
(?:[01]\d|2[0-3])hours 00–23
:[0-5]\dminutes 00–59
(?::[0-5]\d)?$optional seconds

Examples

InputResult
00:00✓ Matches
23:59✓ Matches
14:30:15✓ Matches
24:00✗ No match
9:30✗ No match
12:60✗ No match
12:30:60✗ No match

Every example above is checked automatically in JavaScript and Python each time the site is built.

Limitations and edge cases

Requires a two-digit hour. Use (?:[01]?\d|2[0-3]) to accept 9:30.

When to use this pattern

Scheduling fields, opening hours and log timestamps.

Use it in your language

JavaScript

const re = /^(?:[01]\d|2[0-3]):[0-5]\d(?::[0-5]\d)?$/;
re.test(input); // true or false

Python

import re
pattern = re.compile(r'^(?:[01]\d|2[0-3]):[0-5]\d(?::[0-5]\d)?$')
bool(pattern.search(value))

PHP

$ok = preg_match('~^(?:[01]\\d|2[0-3]):[0-5]\\d(?::[0-5]\\d)?$~', $value) === 1;

Java

Pattern pattern = Pattern.compile("^(?:[01]\\d|2[0-3]):[0-5]\\d(?::[0-5]\\d)?$");
boolean ok = pattern.matcher(value).find();

Go

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

More regex patterns

Frequently asked questions

What is the regex for time (24-hour)?
/^(?:[01]\d|2[0-3]):[0-5]\d(?::[0-5]\d)?$/. Requires a two-digit hour. Use (?:[01]?\d|2[0-3]) to accept 9:30.
Which languages does this pattern work in?
It is tested in JavaScript and Python and uses syntax that also works in PHP (PCRE) and Java. It also works in Go’s RE2 engine.
Why does the pattern start with ^ and end with $?
They anchor the match to the whole string, so "abc00:00xyz" cannot pass just because it contains a valid part. To search for matches inside longer text, remove ^ and $ (use \b where word boundaries help) and add the g flag.