Skip to content

isTimestamp

Checks if a value is a valid timestamp (milliseconds or Unix seconds).

Supports:

  • JavaScript / Java timestamps (milliseconds since epoch)
  • Unix timestamps (seconds since epoch)

The function uses a heuristic to distinguish between the two: numbers ≤ ~7.26 billion are treated as seconds, larger as milliseconds.

Available since v2.0.0

import { isTimestamp } from '@helpers4/guard';
// or, from the all-in-one package (same code, one install):
import { isTimestamp } from 'helpers4/guard';
isTimestamp(value: unknown): value is number
ParameterTypeDescription
valueunknownThe value to check

value is number — True if value is a number that represents a valid timestamp

isTimestamp(1609459200000) // => true (JS ms — 2021-01-01)
isTimestamp(1609459200)    // => true (Unix seconds — 2021-01-01)
isTimestamp(Date.now())    // => true
isTimestamp(NaN)           // => false
isTimestamp('1609459200')  // => false (not a number)

View source on GitHub