Unix Timestamp Regex
Matches a numeric Unix epoch timestamp in either 10-digit seconds precision or 13-digit millisecond precision.
Regex Pattern
^\d{10}(\d{3})?$Pattern Breakdown
Hover over a token to see what it does.
| Token | Meaning |
|---|---|
| ^ | Anchors the match to the start of the string. |
| \d{10} | Exactly ten digits, the length of a seconds-based epoch timestamp until the year 2286. |
| (\d{3})? | An optional three extra digits, extending the value to millisecond precision (13 digits total). |
| $ | Anchors the match to the end of the string. |
Detailed Explanation
What it does
This pattern matches strings that look like a Unix timestamp: a purely numeric value that is either exactly 10 digits (seconds since 1970-01-01T00:00:00Z) or exactly 13 digits (milliseconds since the same epoch). It rejects anything with letters, punctuation, or a length that doesn't match either format.
Why it works
Ten-digit seconds-based timestamps currently fall in a predictable numeric-length range and will continue to do so until the year 2286, so a fixed `\d{10}` reliably identifies them. The optional `(\d{3})?` group appends exactly three more digits when present, which is exactly the extra precision a millisecond timestamp needs, and the anchors ensure no other digit count slips through.
Common use cases
- Validating a timestamp query parameter or path segment in an API
- Detecting whether a numeric log field is a Unix timestamp before converting it to a date
- Filtering structured data exports for epoch-formatted date columns
- Distinguishing between seconds and milliseconds timestamps coming from different systems
Edge cases
- A 9-digit value like 170000000 (year 1975) correctly fails since it's shorter than 10 digits
- An 11 or 12-digit value fails because it matches neither the 10-digit nor 13-digit branch
- Negative timestamps for pre-1970 dates are rejected since the pattern has no sign character
- Timestamps with a decimal fraction, like 1700000000.123, are rejected since the dot isn't part of the character class
Limitations
- Does not verify the value falls within a sane real-world date range, only that it has a plausible digit count
- Cannot distinguish a coincidentally 10 or 13-digit number from an actual timestamp without additional context
- Does not support negative epoch values for dates before 1970
Interactive Tester
Edit the pattern or text below — matching runs live in your browser.
Test Cases
Editable — add your own inputs to see if they pass.
| Input | Expected | Result | |
|---|---|---|---|
| Pass | |||
| Pass | |||
| Pass | |||
| Pass | |||
| Pass | |||
| Pass | |||
| Pass | |||
| Pass |
Language Variants
Production-ready examples in 12 languages.
const unixTimestampPattern = /^\d{10}(\d{3})?$/;
function isUnixTimestamp(value) {
return unixTimestampPattern.test(value);
}
console.log(isUnixTimestamp("1700000000")); // trueCommon Mistakes
Using a bare `^\d+$` to detect timestamps, which also matches unrelated large or small numbers.
Fix: Constrain the digit count to 10 (seconds) or 13 (milliseconds) so only plausible timestamp lengths match.
Forgetting that this pattern only checks shape, not whether the value is a sane, in-range date.
Fix: After the regex passes, convert the value to a Date/DateTime object and confirm it falls within an expected range.
Applying this pattern to timestamps that include a decimal fraction of a second.
Fix: Strip or separately validate the fractional part before matching, or extend the pattern with an optional `(\.\d+)?` group.
Performance Notes
- Fixed-length quantifiers (`{10}`, `{3}`) make matching linear time with no backtracking risk.
- Anchoring both ends lets the engine reject wrong-length strings immediately.
- Checking digit count is far cheaper than parsing the value into a full date object, so use this as a fast pre-filter.
Browser Compatibility
| Engine | Supported | Notes |
|---|---|---|
| Chrome | Yes | — |
| Firefox | Yes | — |
| Safari | Yes | — |
| Edge | Yes | — |
| Node.js | Yes | — |