Date/Time
Time with Seconds (HH:MM:SS) Regex
Log files, media players, sports timing and database exports often record times with seconds, such as 14:30:59. This pattern accepts a 24-hour time from 00:00:00 to 23:59:59, with two digits for each part, and captures hours, minutes and seconds in groups 1 to 3. It is useful for validating time fields in imports, parsing log timestamps after splitting them from the date and checking video chapter markers that use clock time. The honest limit is that it rejects the leap second 23:59:60, fractional seconds and durations longer than a day.
Pattern (JavaScript)
/^([01][0-9]|2[0-3]):([0-5][0-9]):([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 character ":", then a group (one of the characters 0-5, then a digit), then the end of the line/string.
Examples
Matches
- ✓ 00:00:00
- ✓ 14:30:59
- ✓ 23:59:59
Does not match
- ✗ 14:30
- ✗ 24:00:00
- ✗ 14:30:60
Try it
0 matches (JavaScript engine)
00:00:00
14:30:59
23:59:59
14:30
24:00:00
14:30: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]):([0-5][0-9])$/g;
const text = "00: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]):([0-5][0-9])$')
text = '00: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]):([0-5][0-9])$/';
$text = '00: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]):([0-5][0-9])$");
String text = "00: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]):([0-5][0-9])$`)
text := "00: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]):([0-5][0-9])$");
var text = "00: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]):([0-5][0-9])$").unwrap();
let text = "00: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]):([0-5][0-9])$/
text = '00:00:00'
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matchesFrequently asked questions
- How do I allow fractional seconds like 14:30:59.250?
- Add (?:\.[0-9]{1,6})? before the $ anchor. That allows up to six fractional digits, enough for microseconds.
- Can I use this for video timestamps like 1:02:03?
- Video positions are durations, not times of day, and hours are often unpadded. Use a pattern such as ^[0-9]+:[0-5][0-9]:[0-5][0-9]$ instead.
- How do I compute total seconds from a match?
- Multiply group 1 by 3600 and group 2 by 60, then add group 3. The result is seconds since midnight.