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

Unix Timestamps: 6 Bugs That Come From Seconds vs Milliseconds

Timestamps look simple until two systems disagree by a factor of 1000. Here are the bugs I see most often. 1. Seconds vs milliseconds Unix time is seconds since 1970. JavaScript's Date.now() returns milliseco

Timestamps look simple until two systems disagree by a factor of 1000. Here are the bugs I see most often.

1. Seconds vs milliseconds

Unix time is seconds since 1970. JavaScript's Date.now() returns milliseconds. A 10-digit number is seconds, a 13-digit number is milliseconds. Passing seconds to new Date() gives you January 1970.

new Date(1700000000)      // Jan 1970 (wrong)
new Date(1700000000 * 1000) // Nov 2023 (right)

2. Storing local time

Store UTC timestamps and convert to local time only for display. Storing local time breaks as soon as users travel or daylight saving changes.

3. The year 2038 problem

A signed 32-bit integer overflows on 19 January 2038. Use 64-bit columns for any date that may reach past that.

4. Parsing date strings without a zone

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

5. Daylight saving math

Adding 24 hours is not the same as adding one calendar day. On DST change days a day has 23 or 25 hours.

6. Comparing mixed precision

Comparing a seconds value with a milliseconds value is always wrong. Normalise at the boundary of your system.

Quick check

When a date looks wrong, paste the number into a converter and check the digits count. I use the free Unix timestamp converter which runs entirely in the browser, with more tools at webdevtoolbox.com.

πŸ“° 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.