Dev.to WebDev πŸ›  Dev πŸ‘ 0 πŸ“– 1 min read

Unix Timestamp Pitfalls: Seconds, Milliseconds, Timezones and 2038

Unix timestamps look trivial until a bug eats an afternoon. The usual suspects: 1. Seconds vs milliseconds Date.now() in JS returns milliseconds; most backends, JWT exp claims and Unix tools use seconds. A 10

Unix timestamps look trivial until a bug eats an afternoon. The usual suspects:

1. Seconds vs milliseconds

Date.now() in JS returns milliseconds; most backends, JWT exp claims and Unix tools use seconds. A 10-digit value is seconds, 13 digits is milliseconds. Mixing them gives dates in 1970 or the year 50000.

2. Timestamps have no timezone

A Unix timestamp is always UTC. Timezone bugs appear when you format it. Store UTC, convert at the display edge, and keep the user's IANA zone (e.g. Asia/Kolkata), not a fixed offset.

3. The 2038 problem

Signed 32-bit seconds overflow on 2038-01-19. Use 64-bit integers in databases and APIs.

4. Parsing date strings without a zone

new Date("2026-11-21") is parsed as UTC, but new Date("2026-11-21T10:00:00") is local time. Always include Z or an explicit offset.

5. Leap seconds are ignored

Unix time pretends every day has 86400 seconds. Fine for almost everything, but don't use it to measure precise durations across leap seconds.

6. JWT expiry checks

exp and iat are in seconds. Compare against Math.floor(Date.now()/1000).

Quick conversions

To sanity check a value I paste it into a client-side Unix Timestamp converter, which runs locally in the browser.

Disclosure: I built WebDevToolbox, a free collection of 500+ browser-based dev tools.

πŸ“° Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes β€” full credit and traffic to the original publisher.