Date/Time
24-Hour Time (HH:MM) Regex
The 24-hour clock is used in timetables, booking systems, military and aviation contexts and most of the world outside North America. This pattern accepts times from 00:00 to 23:59, requiring two-digit hours and minutes separated by a colon, and captures hours and minutes separately. It is a reliable check for opening hours editors, shift planners, cron-like schedulers and the value of an HTML time input. The honest limit is that it rejects single-digit hours such as 7:30 and the 24:00 notation sometimes used to mean the end of a day.
Pattern (JavaScript)
/^([01][0-9]|2[0-3]):([0-5][0-9])$/Starts at the start of the line/string, then a group (one of the characters 01, then a digit OR the character "2", then one of the characters 0-3), then the character ":", then a group (one of the characters 0-5, then a digit), then the end of the line/string.
Examples
Matches
- ✓ 00:00
- ✓ 13:45
- ✓ 23:59
Does not match
- ✗ 24:00
- ✗ 7:30
- ✗ 12:60
Try it
0 matches (JavaScript engine)
00:00
13:45
23:59
24:00
7:30
12:60Code in 8 languages
Converted automatically from the JavaScript pattern; language-specific notes are shown where the syntax or support differs.
JavaScript
const re = /^([01][0-9]|2[0-3]):([0-5][0-9])$/g;
const text = "00:00";
const matches = [...text.matchAll(re)].map((m) => m[0]);
console.log(matches);Python
import re
pattern = re.compile(r'^([01][0-9]|2[0-3]):([0-5][0-9])$')
text = '00:00'
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
<?php
$pattern = '/^([01][0-9]|2[0-3]):([0-5][0-9])$/';
$text = '00:00';
preg_match_all($pattern, $text, $matches);
print_r($matches[0]);Java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("^([01][0-9]|2[0-3]):([0-5][0-9])$");
String text = "00:00";
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}Go
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`^([01][0-9]|2[0-3]):([0-5][0-9])$`)
text := "00:00"
fmt.Println(re.FindAllString(text, -1))
}C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"^([01][0-9]|2[0-3]):([0-5][0-9])$");
var text = "00:00";
foreach (Match m in pattern.Matches(text))
{
Console.WriteLine(m.Value);
}Rust
// Cargo.toml: regex = "1"
use regex::Regex;
fn main() {
let re = Regex::new(r"^([01][0-9]|2[0-3]):([0-5][0-9])$").unwrap();
let text = "00:00";
for m in re.find_iter(text) {
println!("{}", m.as_str());
}
}- Add regex = "1" under [dependencies] in Cargo.toml.
Ruby
pattern = /^([01][0-9]|2[0-3]):([0-5][0-9])$/
text = '00:00'
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matchesFrequently asked questions
- How do I accept 7:30 without a leading zero?
- Change the hour group to ([01]?[0-9]|2[0-3]). That allows one-digit hours from 0 to 9 while keeping the range up to 23.
- Why is 24:00 rejected?
- Most systems represent midnight as 00:00 on the following day. If your domain uses 24:00 for closing times, add |24:00 as an extra alternative inside the anchors.
- Can I convert the captured parts into minutes?
- Yes, multiply group 1 by 60 and add group 2. That gives minutes since midnight, which is convenient for comparing or sorting times.