Date/Time
Unix Timestamp Regex
Unix timestamps count time since 1 January 1970 UTC. Values in seconds have ten digits for dates between 2001 and 2286, and values in milliseconds, as returned by JavaScript's Date.now(), have thirteen. This pattern accepts exactly those two lengths, which helps APIs, log parsers and debugging tools tell whether a field holds seconds or milliseconds. The honest limit is that timestamps before September 2001 have fewer than ten digits and are rejected, and any ten-digit number, such as a phone number without separators, also passes the check.
Pattern (JavaScript)
/^[0-9]{10}(?:[0-9]{3})?$/Starts at the start of the line/string, then a digit, exactly 10 times, then (a digit, exactly 3 times), optionally (zero or one time), then the end of the line/string.
Examples
Matches
- ✓ 1764460800
- ✓ 1764460800123
- ✓ 1000000000
Does not match
- ✗ 176446080
- ✗ 17644608001
- ✗ 2026-11-30
Try it
0 matches (JavaScript engine)
1764460800
1764460800123
1000000000
176446080
17644608001
2026-11-30Code in 8 languages
Converted automatically from the JavaScript pattern; language-specific notes are shown where the syntax or support differs.
JavaScript
const re = /^[0-9]{10}(?:[0-9]{3})?$/g;
const text = "1764460800";
const matches = [...text.matchAll(re)].map((m) => m[0]);
console.log(matches);Python
import re
pattern = re.compile(r'^[0-9]{10}(?:[0-9]{3})?$')
text = '1764460800'
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
<?php
$pattern = '/^[0-9]{10}(?:[0-9]{3})?$/';
$text = '1764460800';
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("^[0-9]{10}(?:[0-9]{3})?$");
String text = "1764460800";
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}Go
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`^[0-9]{10}(?:[0-9]{3})?$`)
text := "1764460800"
fmt.Println(re.FindAllString(text, -1))
}C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"^[0-9]{10}(?:[0-9]{3})?$");
var text = "1764460800";
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"^[0-9]{10}(?:[0-9]{3})?$").unwrap();
let text = "1764460800";
for m in re.find_iter(text) {
println!("{}", m.as_str());
}
}- Add regex = "1" under [dependencies] in Cargo.toml.
Ruby
pattern = /^[0-9]{10}(?:[0-9]{3})?$/
text = '1764460800'
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matchesFrequently asked questions
- How can I tell seconds from milliseconds?
- Count the digits: ten digits means seconds and thirteen means milliseconds for current dates. Wrap the optional group in a capture to know which form matched.
- Why are 11 and 12 digit values rejected?
- Those lengths do not correspond to current dates in either seconds or milliseconds, so they usually mean a value was truncated or padded. The pattern rejects them to catch such errors.
- Does this support negative timestamps?
- No. Dates before 1970 have negative timestamps, and older dates have fewer digits. Use a broader pattern like ^-?[0-9]+$ and range checks in code if you need them.
Related patterns
- ISO 8601 Date-Time^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])T([01][0-9]|2[0-3]):[0-5][0-9](:[0-5][0-9](\.[0-9]{1,9})?)?(Z|[+-]([01][0-9]|2[0-3]):?[0-5][0-9])?$
- ISO 8601 Date (YYYY-MM-DD)^([0-9]{4})-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])$
- 12-Hour Time with AM/PM^(0?[1-9]|1[0-2]):[0-5][0-9] ?[AaPp][Mm]$
- 24-Hour Time (HH:MM)^([01][0-9]|2[0-3]):([0-5][0-9])$