Regex for Time in hh:mm: the Pattern That Actually Holds Up
The most common regex to validate time in hh:mm format is \d{1,2}:\d{2}, and it is wrong. It checks the shape, digits, colon, digits, but not the ranges, so it happily accepts 99:99 as a time. The fix is not longer. It is more specific, and it comes from the Regular Expressions Cookbook, which solved this properly years ago.
Think about what a valid time actually constrains. Hours run 00 to 23, not 00 to 99. Minutes run 00 to 59, not 00 to 99. The naive pattern treats both slots as "some digits," which is why it passes garbage. Validating time is easier than validating dates, as the Cookbook notes, because every hour has 60 minutes and every minute has 60 seconds: the ranges are uniform, so the pattern does not need the messy month-day-year logic that dates demand.
The hours piece is where the work happens. 2[0-3] matches 20 through 23, the hours that need a 2 in front. [01]?[0-9] matches 0 through 19 with the leading zero optional. Put them in an alternation and you have every hour from 0 to 23 and nothing else: (2[0-3]|[01]?[0-9]). The minutes piece is simpler. [0-5]?[0-9] matches any number from 0 to 59, because the first digit is capped at 5. Anchor both ends and add the colon, and you get the whole thing:
^(2[0-3]|[01]?[0-9]):([0-5]?[0-9])$
That pattern is from Goyvaerts and Levithan's Regular Expressions Cookbook, second edition, and it is flavor-agnostic: .NET, Java, JavaScript, PCRE, Perl, Python, and Ruby all handle it the same way. The question marks make leading zeros optional, so 9:41 and 09:41 both match. If your input comes from a form that always pads, remove the question marks and 9:41 fails. That is a choice about your users, not about regex.
The 12-hour version adds one more alternation and an optional marker. Hours run 01 to 12 instead of 00 to 23, and AM or PM rides along at the end:
^(1[0-2]|0?[1-9]):([0-5]?[0-9])( ?[AP]M)?$
One thing this pattern deliberately does not decide, and I like that about it. The AM/PM group is optional, so it accepts both 9:41 and 9:41 PM. Whether a bare time without a marker should count as valid is a business rule, not a regex problem. If your system treats "no marker" as invalid, make the group required. If it defaults to AM, leave it. The regex handles the shape; your code handles the meaning.
One more mistake to avoid, because it bites people who copy the pattern into larger expressions. The ^ and $ anchors are load-bearing: without them, the pattern matches anywhere inside a string, so "meet at 9:41 tomorrow" would pass validation as a "time." Keep the anchors when you are validating a whole input. Drop them only when you are searching for times inside larger text, and then expect partial matches.
A loose end worth mentioning before you ship this: 24:00. Train schedules and broadcast listings use it to mean midnight at the end of the day, and the strict pattern above rejects it. Whether that is a bug or a feature depends on whether your users type it. I have gone back and forth on whether validation should be generous about human conventions or strict about the clock, and the honest answer is that the strict version is right for machine-generated input and wrong for human-typed input, and most real systems get a mix of both. If 24:00 shows up in your data, add an explicit alternative for it rather than loosening the hour range.
Frequently asked questions
What is the regex for 24-hour time hh:mm?
^(2[0-3]|[01]?[0-9]):([0-5]?[0-9])$ from the Regular Expressions Cookbook. The hours part accepts 00 to 23, the minutes part 00 to 59. Works across .NET, Java, JavaScript, PCRE, Perl, Python, and Ruby.
Why does my time regex accept 99:99?
Because \d{1,2}:\d{2} checks the shape (digits, colon, digits) but not the ranges. \d matches any digit, so 99:99 is two digits, a colon, and two digits. Real validation needs the hours limited to 00-23 and the minutes to 00-59.
Should leading zeros be required in a time regex?
Depends on your input. In the Cookbook patterns, the question marks after the first character classes make leading zeros optional, so 9:41 and 09:41 both match. Remove the question marks to require them: 9:41 would then fail. Pick based on what your users actually type.
What is the regex for 12-hour time with AM and PM?
^(1[0-2]|0?[1-9]):([0-5]?[0-9])( ?[AP]M)?$ from the Regular Expressions Cookbook. Hours run 01 to 12, minutes 00 to 59, and the AM/PM marker is optional. Note it accepts both 9:41 and 9:41 PM; tightening that up is a business rule, not a regex problem.
Related reading: Why Your Date Validation Regex Fails · Regex for Postal Code Validation · Regex Word Boundaries, Explained · Email Regex: the Mistakes Everyone Makes · The Regex Multiline Flag, Explained