Regex for a time (HH:MM)

A regular expression for 24-hour times such as 09:05 and 23:59, with variants for seconds and for 12-hour clock times with AM and PM.

//

Matches

This tool runs in your browser. We never send what you type or paste to a server.

The pattern

/^(?:[01][0-9]|2[0-3]):[0-5][0-9]$/

This is the strict 24-hour form, with two digits for the hour and two for the minute, as used in ISO 8601, RFC 3339 and the value of an HTML <input type="time">. The hour is limited to 00 to 23 and the minute to 00 to 59.

The tester at the top of this page is loaded with this pattern and the examples below, one per line. It adds the m flag so that ^ and $ apply to each line, and g to show every match. Edit either box to try your own input.

How it works, token by token

PartMeaning
^Start of the string.
(?:[01][0-9]|2[0-3])The hour, 00 to 23: either 0 or 1 followed by any digit, or 2 followed by 0 to 3.
:A literal colon.
[0-5][0-9]The minute, 00 to 59.
$End of the string.

What it matches and what it rejects

InputResultWhy
00:00matchesMidnight.
09:05matchesLeading zeros.
13:45matchesAfternoon.
23:59matchesThe last minute of the day.
24:00rejectedHour 24.
9:05rejectedA one-digit hour (see below to allow it).
12:60rejectedMinute 60.
12:5rejectedA one-digit minute.
1245rejectedNo colon.
12.45rejectedA dot.
12:45:30rejectedSeconds: use the next pattern.

With seconds

Add an optional group for seconds:

/^(?:[01][0-9]|2[0-3]):[0-5][0-9](?::[0-5][0-9])?$/
InputResultWhy
13:45matchesSeconds are optional.
13:45:30matchesWith seconds.
13:45:60rejectedSecond 60.
13:45:3rejectedOne digit.

RFC 3339 allows a seconds value of 60 for a leap second, as in 23:59:60. Few systems ever produce one, and most date libraries reject it, so this pattern does too. The leap seconds guide explains why Unix time skips them.

12-hour times with AM and PM

A 12-hour clock runs from 12 through 1 to 11, so the hour is 1 to 12, here with an optional leading zero, and an AM or PM marker is required. The i flag accepts lowercase:

/^(?:0?[1-9]|1[0-2]):[0-5][0-9] ?[AP]M$/i
InputResultWhy
9:05 ammatchesOne-digit hour, lowercase.
09:05 AMmatchesTwo digits.
12:00 PMmatchesNoon.
11:59pmmatchesNo space.
13:00 PMrejectedThere is no 13 on a 12-hour clock.
00:30 AMrejectedNor a 0.
12:00rejectedAM or PM is required.
9:05 a.m.rejectedDots are not allowed here.

Midnight is 12:00 AM and noon is 12:00 PM, which trips up conversion code more often than the pattern does. To convert to 24-hour time, take the hour modulo 12 and add 12 for PM.

Allowing one-digit hours

People often type 9:05. To accept it in 24-hour time, change the hour part to (?:[01]?[0-9]|2[0-3]), then pad the hour to two digits before you store it so that times still sort correctly as text. Keep the minutes at two digits: 9:5 is more likely a typing mistake than 09:05.

A time without a date and a time zone is ambiguous when the clocks change: in the UK, 01:30 happens twice on the last Sunday of October and not at all on the last Sunday of March. If the time describes a real moment, store it with a date and a zone, or as a Unix timestamp.

In JavaScript, Python, PHP and C#

JavaScript

const re = /^(?:[01][0-9]|2[0-3]):[0-5][0-9]$/;
console.log(re.test('00:00')); // → true
console.log(re.test('24:00')); // → false

Python

import re

pattern = re.compile(r'(?:[01][0-9]|2[0-3]):[0-5][0-9]')
print(bool(pattern.fullmatch('00:00')))  # → True
print(bool(pattern.fullmatch('24:00')))  # → False

PHP

<?php
$pattern = '/^(?:[01][0-9]|2[0-3]):[0-5][0-9]$/D';
echo preg_match($pattern, '00:00'), "\n"; // → 1
echo preg_match($pattern, '24:00'), "\n"; // → 0

C#

using System;
using System.Text.RegularExpressions;

var pattern = new Regex(@"^(?:[01][0-9]|2[0-3]):[0-5][0-9]\z");
Console.WriteLine(pattern.IsMatch("00:00")); // → True
Console.WriteLine(pattern.IsMatch("24:00")); // → False

Each version checks the whole string. JavaScript's $ only matches at the very end, but Python, PHP and .NET also let $ match just before a final line break, so "abc\n" could slip through. The Python version uses re.fullmatch (so the ^ and $ are dropped), PHP adds the D modifier, and C# ends with \z.

The JavaScript, Python and C# examples are run before each update and their output is compared with the comments. PHP is not installed on our build machine, so the PHP version is checked against the PCRE pattern modifier documentation, and its string literal is checked to hand PHP exactly the pattern tested here.

Questions

What is the regex for 24-hour time?

^(?:[01][0-9]|2[0-3]):[0-5][0-9]$ matches 00:00 to 23:59. Add (?::[0-5][0-9])? before the $ to allow seconds.

Is 24:00 a valid time?

Some standards have used 24:00 for the end of a day, but the HTML time input and most libraries only accept 00:00 to 23:59, and this pattern does the same.

How do I match a time with optional AM or PM?

Use separate patterns for 12-hour and 24-hour input, and decide by whether AM or PM is present. One pattern that allows both would accept nonsense such as 18:00 PM.