DD
DevDash

Regex for Time with Milliseconds

Regex Pattern

^([01]\d|2[0-3]):([0-5]\d):([0-5]\d)\.\d{1,3}$

Time in HH:MM:SS.mmm format with milliseconds

Quick Answer

The regex pattern for time with milliseconds is `^([01]\d|2[0-3]):([0-5]\d):([0-5]\d)\.\d{1,3}$`. Time in HH:MM:SS.mmm format with milliseconds. This works in JavaScript, Python, Ruby, PHP, Java, and most regex engines that support PCRE syntax.

Test Examples

InputResult
14:30:00.123✓ Matches
00:00:00.0✓ Matches
23:59:59.999✓ Matches
14:30:00✗ No match
24:00:00.000✗ No match
14:30✗ No match

Code Examples

javascript

const regex = /^([01]\d|2[0-3]):([0-5]\d):([0-5]\d)\.\d{1,3}$/;
const isValid = regex.test(value);

python

import re
pattern = r'^([01]\d|2[0-3]):([0-5]\d):([0-5]\d)\.\d{1,3}$'
if re.match(pattern, value):
    print("valid")

ruby

pattern = /^([01]\d|2[0-3]):([0-5]\d):([0-5]\d)\.\d{1,3}$/
if value =~ pattern
  puts "valid"
end

php

if (preg_match('/^([01]\d|2[0-3]):([0-5]\d):([0-5]\d)\.\d{1,3}$/', $value)) {
    echo "valid";
}

java

String pattern = "^([01]\\d|2[0-3]):([0-5]\\d):([0-5]\\d)\\.\\d{1,3}$";
boolean isValid = value.matches(pattern);

Frequently Asked Questions

Related Regex Patterns

Want API access + no ads? Pro coming soon.