Regex for time (24-hour HH:MM)

Use /^(?:[01]\d|2[0-3]):[0-5]\d(?::[0-5]\d)?$/ to check that a whole string is a valid for time (24-hour HH:MM). Every example below is tested against the pattern, and you can open it in the Regex Tester with the samples preloaded.

Updated

/^(?:[01]\d|2[0-3]):[0-5]\d(?::[0-5]\d)?$/
Open in Regex Tester
InputResult
00:00✓ Match
23:59✓ Match
14:30:15✓ Match
09:05✓ Match
24:00✗ No match
9:30✗ No match
12:60✗ No match
12:30:60✗ No match

How the pattern works

PartMeaning
(?:[01]\d|2[0-3])Hours 00–23
:A colon
[0-5]\dMinutes 00–59
(?::[0-5]\d)?Optional seconds

Code examples

JavaScript

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

Python

import re

bool(re.fullmatch(r'(?:[01]\d|2[0-3]):[0-5]\d(?::[0-5]\d)?', value))

PHP

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

Notes and limitations

Make the leading zero optional with (?:[01]?\d|2[0-3]) if users type 9:30.

Regex TesterType a pattern and watch matches highlight live in your test text.
Open the tool

Frequently asked questions

What is the regex for time (24-hour HH:MM)?
/^(?:[01]\d|2[0-3]):[0-5]\d(?::[0-5]\d)?$/ — it matches the whole string. See the table above for what each part does.
Which examples match?
Of the examples tested, these match: 00:00, 23:59, 14:30:15, 09:05. The others don’t.
How do I test this regex?
Click “Open in Regex Tester” — the pattern, flags and sample text load automatically, and matches highlight as you edit.