You fetch a record from an API, pass its created_at to new Date(), and the page says January 21, 1970. Or a scheduled job runs five and a half hours early. Or a date picker shows yesterday.
Almost every one of these comes from a handful of timestamp mix-ups. Here they are, with the fix for each.
1. Seconds vs milliseconds (the 1970 bug)
A Unix timestamp counts time since 1 January 1970, 00:00 UTC. The catch is the unit:
- Most backends, databases and JWTs use seconds:
1795686000(10 digits today) - JavaScript's
Dateuses milliseconds:1795686000000(13 digits)
Pass seconds to new Date() and JavaScript reads them as milliseconds, which is only about 20 days after the epoch:
new Date(1795686000); // 1970-01-21... wrong
new Date(1795686000 * 1000); // 2026-11-26... right
The opposite mistake sends seconds-based APIs a 13-digit number, which lands tens of thousands of years in the future.
A small helper that guesses the unit by size:
function toDate(ts) {
const n = Number(ts);
// Below 1e11 it's almost certainly seconds (1e11 ms is only 1973)
return new Date(n < 1e11 ? n * 1000 : n);
}
And the reverse, for sending a timestamp to an API that expects seconds:
const nowSeconds = Math.floor(Date.now() / 1000);
Use Math.floor, not Math.round. Rounding up can create a time slightly in the future, which some APIs reject.
2. Timestamps have no time zone. Your display does.
A timestamp is a single moment, the same everywhere. Time zones only appear when you turn it into text:
const d = new Date(1795686000 * 1000);
d.toISOString(); // always UTC, ends in "Z"
d.toLocaleString(); // the viewer's local time
d.toLocaleString('en-IN', { timeZone: 'Asia/Kolkata' }); // a fixed zone
If your server logs show UTC and your users see IST, a "5:30 hour bug" is usually just two correct times in two zones. Store and send timestamps (or ISO strings with Z), and convert to local time only at the very end, in the UI.
3. The date-only string trap
This one surprises even experienced developers:
new Date('2026-11-26'); // parsed as UTC midnight
new Date('2026-11-26T00:00'); // parsed as LOCAL midnight
A date-only ISO string is treated as UTC, but a date-time string without an offset is treated as local time. For someone in the US, the first line is still the evening of 25 November in local time, so a calendar shows the wrong day.
If you mean a calendar date (a birthday, a due date), keep it as the string '2026-11-26' and don't turn it into a Date at all. If you need a Date, add the time and offset explicitly.
4. The year 2038
Systems that store seconds in a signed 32-bit integer overflow on 19 January 2038 at 03:14:07 UTC. JavaScript itself is fine, since Date uses 64-bit floating point milliseconds. But if your data passes through an old database column, an embedded device or a C library, check the column type now. Use a 64-bit integer or a proper timestamp type.
Quick checklist
- Count the digits: 10 means seconds, 13 means milliseconds.
- Multiply by 1000 going into
Date, divide (and floor) coming out. - Store and transmit in UTC; format to local time only for display.
- Don't parse date-only strings into
Dateunless you mean UTC.
Try it
When I'm debugging, I paste the number into SnipFerry's timestamp converter to see the date it really points to. It runs in the browser, so nothing gets sent anywhere.
What's the worst date bug you've shipped?
Top comments (0)