Pin the definition down first, or everything after it drifts
A Unix timestamp is “seconds elapsed since 1970-01-01T00:00:00Z”. Every word in that sentence is load-bearing, especially the trailing Z — the origin is a UTC instant, not midnight on New Year’s Day 1970 in some particular place. From which follows a conclusion this article keeps returning to: a timestamp carries no time zone. One timestamp corresponds to one instant worldwide. There is no such thing as “a Beijing timestamp” and “a New York timestamp”.
So of the five traps below, three — time zones, zero-based months, seconds versus milliseconds — all occur at the two boundaries between a timestamp and a human-readable form: parsing (string to timestamp) and formatting (timestamp to string). The other two sit elsewhere. Leap seconds are a defect in the time scale itself: Unix time simply declines to number that extra second, at the cost of the same timestamp value occurring twice. 2038 is about storage width and integer arithmetic: a 32-bit container cannot hold the number. The three in the title are the hardest to reason out unaided; the other two are cheap to fix but catch far more people.
Every output below was actually run on Node v22.17.1 / macOS 26.5.2, system time zone Asia/Shanghai (UTC+8, no daylight saving in China). Anything time-zone-dependent is labelled with its TZ. Run it in another zone and the numbers should change — if they do not, something is wrong.
Trap zero: seconds or milliseconds, and why “count the digits” only works between 2001 and 2286
“Ten digits means seconds, thirteen means milliseconds” is a rule almost everyone uses, including the timestamp converter on this site. It is useful, but the conditions under which it holds are rarely spelled out.
Start with a precise mathematical fact: for the same instant, a millisecond timestamp always has exactly three more digits than a second timestamp. The proof is one line. If 10^(k−1) ≤ s < 10^k, then the millisecond value lies in [1000s, 1000s+999], and 1000s ≥ 10^(k+2) while 1000s+999 ≤ 1000(10^k − 1) + 999 = 10^(k+3) − 1 < 10^(k+3). So the millisecond value has exactly k+3 digits. I also checked it exhaustively over 200,000 random timestamps; counterexamples: 0.
let bad = 0;
for (let i = 0; i < 200000; i++) {
const s = Math.floor(Math.random() * 4e9) + 1;
const ms = s * 1000 + Math.floor(Math.random() * 1000);
if (String(ms).length !== String(s).length + 3) bad++;
}
console.log('counterexamples:', bad); // counterexamples: 0
Look carefully at what that fact says. It is a relative rule. It guarantees “milliseconds has three more digits than seconds”. It does not guarantee “seconds has ten digits”. And the engineering rule — ten means seconds, thirteen means milliseconds — is exactly the relative rule used as an absolute one. It quietly assumes your timestamp falls inside one particular range of years. That range has computable boundaries:
| Second timestamp | Millisecond timestamp | Covers (UTC) |
|---|---|---|
| 9 digits | 12 digits | from 1973-03-03T09:46:40Z |
| 10 digits | 13 digits | from 2001-09-09T01:46:40Z |
| 11 digits | 14 digits | from 2286-11-20T17:46:40Z |
The row worth remembering is the middle one. Because 1000 × 10⁹ = 10¹², 10⁹ seconds and 10¹² milliseconds denote the same instant, and both representations roll over together at that moment:
new Date(1000000000 * 1000).toISOString(); // '2001-09-09T01:46:40.000Z'
new Date(1000000000000).toISOString(); // '2001-09-09T01:46:40.000Z'
new Date(999999999 * 1000).toISOString(); // '2001-09-09T01:46:39.000Z' 9 digits
new Date(999999999999).toISOString(); // '2001-09-09T01:46:39.999Z' 12 digits
2001-09-09T01:46:40Z is the moment “guess by digit count” starts working. Before it, a second timestamp has at most 9 digits and a millisecond timestamp at most 12 — neither reaches the 13-digit threshold, so the digit rule misclassifies every millisecond value as seconds. This is not theoretical; any system holding dates before 2001 runs into it:
Date.UTC(2000, 0, 1); // 946684800000 (12 digits)
new Date(946684800000 * 1000).toISOString(); // '+031969-04-01T00:00:00.000Z'
Date.UTC(1980, 4, 15); // 327196800000 (12 digits)
new Date(327196800000 * 1000).toISOString(); // '+012338-06-16T00:00:00.000Z'
The year 31969 is at least loud — obviously wrong at a glance. The insidious case is the ambiguous band: a 10-digit value read as seconds lands between 2001-09-09T01:46:40Z and 2286-11-20T17:46:39Z, and read as milliseconds lands between 1970-01-12T13:46:40Z and 1970-04-26T17:46:39.999Z. Both readings produce a syntactically valid, semantically plausible date — and the January 1970 shape looks exactly like “this field was never populated”. No error is raised. You just find a small batch of records inexplicably clustered near the epoch in some report.
new Date(1000000000 * 1000).toISOString(); // '2001-09-09T01:46:40.000Z'
new Date(1000000000).toISOString(); // '1970-01-12T13:46:40.000Z'
Negative timestamps break the digit method outright. -1 is two characters and denotes 1969-12-31T23:59:59Z; anything before the epoch is negative, and digit count stops carrying magnitude information entirely.
A sturdier approach is to compute both and choose by whether the result lands in a plausible window of years, rather than counting digits:
function guessEpoch(n, lo = Date.UTC(1990, 0, 1), hi = Date.UTC(2100, 0, 1)) {
const okSec = n * 1000 >= lo && n * 1000 <= hi;
const okMs = n >= lo && n <= hi;
if (okSec && !okMs) return { unit: 's', date: new Date(n * 1000) };
if (okMs && !okSec) return { unit: 'ms', date: new Date(n) };
if (okMs && okSec) return { unit: 'ambiguous' };
return { unit: 'unknown' };
}
This turns guessing into an explicit windowing assumption — the window is yours, written down, and both ambiguity and out-of-range are reported separately rather than silently resolved into a date. The price is that it is not universal: the 1972 second timestamp 63072000 returns unknown because it falls outside the window. That is deliberate. Admitting you cannot tell beats guessing wrong.
A real incident shape, while we are here. JWT’s exp is a NumericDate per RFC 7519 — seconds. Put Date.now() (milliseconds) in it, say 1788998400000, and read as seconds that is the year 58661. The value is inside JavaScript’s Date range, nothing errors, and the token has quietly become one that never expires. The reverse is just as bad: read the second value 1788998400 as milliseconds and you get 1970-01-21T16:56:38.400Z, so the token is permanently expired and every request 401s.
Trap one: the time zone was never in the timestamp
This is the point this article most wants to land. “Timestamps have a time zone problem” is backwards — a timestamp is a bare integer precisely in order to eliminate time-zone ambiguity. The same millisecond timestamp 1788998400000 renders as five different strings in five zones, while getTime() returns the same number in every one:
TZ |
Local rendering (toLocaleString('sv-SE')) |
getTime() |
|---|---|---|
UTC |
2026-09-10 00:00:00 | 1788998400000 |
Asia/Shanghai |
2026-09-10 08:00:00 | 1788998400000 |
America/New_York |
2026-09-09 20:00:00 | 1788998400000 |
Pacific/Kiritimati |
2026-09-10 14:00:00 | 1788998400000 |
Pacific/Midway |
2026-09-09 13:00:00 | 1788998400000 |
Five rows spanning two calendar dates and 25 hours of clock time, with the timestamp unmoved. So every “time zone bug” you meet in production is necessarily located at parsing or formatting. Looking for the cause inside the timestamp will never find anything.
The most famous trap at that boundary is specified behaviour in ECMAScript, not an engine quirk: date-only strings parse as UTC; strings carrying a time but no offset parse as local time.
// TZ=Asia/Shanghai node
const a = new Date('2026-09-10');
const b = new Date('2026-09-10T00:00:00');
console.log(a.getTime(), a.toISOString(), a.toString());
console.log(b.getTime(), b.toISOString(), b.toString());
console.log((a.getTime() - b.getTime()) / 3600000);
Actual output:
1788998400000 2026-09-10T00:00:00.000Z Thu Sep 10 2026 08:00:00 GMT+0800 (China Standard Time)
1788969600000 2026-09-09T16:00:00.000Z Thu Sep 10 2026 00:00:00 GMT+0800 (China Standard Time)
8
To a human the two strings say the same thing. The parsed timestamps differ by 28,800,000 ms — exactly 8 hours. a is 8 hours later than b, because a is pinned to UTC midnight (08:00 local) and b to local midnight (16:00 UTC the previous day).
One detail here deserves attention: in UTC+8, both have getDate() of 10. The date looks right. The difference shows up only as an 8-hour offset, and an 8-hour offset does not immediately surface in a lot of business logic. Move to a negative offset and the same code reveals an off-by-one-day:
// TZ=America/New_York node
new Date('2026-09-10').toString();
// 'Wed Sep 09 2026 20:00:00 GMT-0400 (Eastern Daylight Time)'
new Date('2026-09-10').getDate(); // 9
new Date('2026-09-10T00:00:00').getDate(); // 10
The same code is an 8-hour skew on a UTC+8 machine and a wrong-day bug on a UTC−4 one. This is why such bugs so often present as “works locally, breaks in production” — except production servers usually run UTC, where the offset is 0 and both parses agree exactly. So it works there too, and only breaks in users’ browsers.
The same trap has a mirror image on the way back out. The front end holds a Date at local midnight, wants to store YYYY-MM-DD, and reaches for toISOString():
// TZ=Asia/Shanghai node
new Date('2026-09-10T00:00:00').toISOString().slice(0, 10); // '2026-09-09'
new Date('2026-09-10').toISOString().slice(0, 10); // '2026-09-10'
The user picked 10 September; the database received 9 September. toISOString() always emits UTC, and local midnight falls on the previous UTC day in every positive-offset zone.
List the common forms together and the line the spec draws becomes visible:
| String | Parsed timestamp | Equivalent UTC instant |
|---|---|---|
'2026-09-10' |
1788998400000 | 2026-09-10T00:00:00Z |
'2026-09-10T00:00:00Z' |
1788998400000 | 2026-09-10T00:00:00Z |
'2026-09-10T00:00:00' |
1788969600000 | 2026-09-09T16:00:00Z |
'2026-09-10T00:00' |
1788969600000 | 2026-09-09T16:00:00Z |
'2026-09-10T00:00:00+08:00' |
1788969600000 | 2026-09-09T16:00:00Z |
'2026/09/10' |
1788969600000 | 2026-09-09T16:00:00Z |
'2026-9-10' |
1788969600000 | 2026-09-09T16:00:00Z |
'Sep 10 2026' |
1788969600000 | 2026-09-09T16:00:00Z |
The first five are spec-defined ISO 8601 forms with guaranteed behaviour. The last three — slash-separated, unpadded month, English month name — are not ISO, and the spec lets engines parse them however they like. On this machine they all went to local time. That result is not portable; another engine may parse them differently, or return Invalid Date. Do not hand non-ISO strings to new Date(). That is a portability issue, not a style preference.
The prescription is three sentences, all of which must be followed. Store and transmit integers or ISO strings carrying Z. When parsing, always let the string carry its own offset — never assume the runtime’s zone is the one you think. Introduce a time zone only at the final rendering step, and specify it explicitly (toLocaleString’s timeZone option) rather than relying on the system default.
Trap two: leap seconds, which Unix time flatly pretends do not exist
A leap second is one second inserted by the IERS to keep UTC in step with the Earth’s rotation. It is inserted by giving some day’s final minute 61 seconds: after 23:59:59 comes 23:59:60, and only then 00:00:00 the next day.
Unix time deals with this very directly: it does not count leap seconds. POSIX specifies exactly 86,400 seconds per day, with the timestamp being days × 86400 + seconds-of-day. There is no slot for an 86,401st second. The cost of that sentence is quantifiable:
const t1972 = Date.UTC(1972, 0, 1) / 1000; // 63072000
const t2017 = Date.UTC(2017, 0, 1) / 1000; // 1483228800
console.log(t2017 - t1972); // 1420156800
console.log((t2017 - t1972) / 86400); // 16437 (whole days, not one second more)
Between 1972-01-01 and 2017-01-01 the Unix scale advanced 1,420,156,800 seconds — exactly 16,437 whole days. The atomic seconds that actually elapsed over that span number 1,420,156,827, because TAI−UTC was 10 seconds in 1972 and 37 by 2017, with 27 leap seconds inserted in between. Those 27 seconds simply do not exist on the Unix axis. They were not compressed, they were skipped: the conversion between Unix time and atomic time is not a constant and not a formula, but a historical table you have to look up.
At the instant of a leap second, the question becomes “what number does 23:59:60 get”. The answer is: none.
Date.UTC(2016, 11, 31, 23, 59, 59) / 1000; // 1483228799
Date.UTC(2017, 0, 1) / 1000; // 1483228800
new Date('2016-12-31T23:59:60Z').toString(); // 'Invalid Date'
Two adjacent numbers one apart, with nowhere to put the second in between. Implementations have exactly two options, each with consequences.
Repeat the timestamp: the value 1483228799 occurs twice, for two seconds. Three intuitions break at once — timestamps stop being monotonic; two log lines can share an identical timestamp without being simultaneous; and an interval measured by subtracting two timestamps can come out as zero or negative.
Smear it: do not insert the second, but run the local clock slightly slow across a window so that it falls one second behind naturally. The scheme Google recommends and runs on its public NTP is a 24-hour linear smear centred on the leap instant, from noon UTC to noon the next day. The frequency deviation is 1/86400, about 11.57 ppm — smaller than the error of most quartz oscillators, so applications barely notice. The cost is that during the window this machine’s clock is not equal to true UTC: over a 24-hour centred linear smear the accumulated difference is one second, peaking at 0.5 s at the leap instant and returning to zero at both ends. And if some machines in a cluster smear while others step, the two disagree for that whole day.
From which comes a discipline more general than leap seconds themselves: never measure an interval by subtracting two wall-clock timestamps. Leap seconds, NTP corrections and users setting their clocks by hand can all move the wall clock backwards. (Daylight saving is not in this list — it changes only how local time is rendered, not the timestamp.) Measure intervals with a monotonic clock: performance.now() in browsers and Node, CLOCK_MONOTONIC on Linux.
Now the current status, which has changed materially since this article’s Chinese original and is worth stating carefully:
- The most recent leap second was 2016-12-31T23:59:60 UTC. None has been inserted since.
- IERS Bulletin C 72, dated 6 July 2026, announced no leap second at the end of 2026 and confirmed “from 2017 January 1, until further notice, UTC−TAI = −37 s”.
- The 27th CGPM (2022) Resolution 4 decided that the maximum value of the UT1−UTC difference would be increased “in, or before, 2035”. Widening the tolerance is the mechanism by which leap seconds stop being needed — note that the resolution adopted was not the phrase “abolish the leap second” but that single act of changing a limit.
- The 28th CGPM, meeting 13–15 October 2026, adopted Resolution 5 on 15 October 2026. Reporting on it says the resolution ends the insertion of leap seconds and raises the maximum UT1−UTC difference to 3,600 seconds — one hour — with continuous UTC taking effect on 20 May 2027. I have not read the official CGPM text, so the 3,600-second figure here rests on secondary reporting; treat the exact number as needing confirmation against the primary source, even though the direction is not in doubt.
For anyone writing code, the conclusions are: leap seconds can only ever be inserted at the end of June or December; end-2026 was already excluded by Bulletin C 72; and from 20 May 2027 none will be inserted again. But your historical data already carries the holes left by 27 of them — and “timestamps increase monotonically” was never an assumption that should have rested on leap seconds not happening, because NTP correction does the same thing at any moment.
Trap three: 2038-01-19 03:14:07 UTC
The largest value a signed 32-bit integer holds is 2³¹ − 1 = 2147483647. Interpreted as a second timestamp:
new Date(2147483647 * 1000).toISOString(); // '2038-01-19T03:14:07.000Z'
That is the precise instant of the 2038 problem. Add one second and a signed 32-bit container wraps to −2³¹:
const i32 = new Int32Array(1);
i32[0] = 2147483647;
new Date(i32[0] * 1000).toISOString(); // '2038-01-19T03:14:07.000Z'
i32[0] = 2147483648; // does not fit, wraps
console.log(i32[0]); // -2147483648
new Date(i32[0] * 1000).toISOString(); // '1901-12-13T20:45:52.000Z'
The clock does not stop; it jumps back to 13 December 1901 — and that direction matters. Affected systems will not freeze at 2038; they will abruptly believe it is the early twentieth century, so every “has expired” and “is in the future” judgement inverts simultaneously. Incidentally the local rendering of that instant varies: 2038-01-19 11:14:07 (+0800) in Asia/Shanghai, 2038-01-18 22:14:07 (−0500) in America/New_York — the same instant either way.
Three places this trap is still alive. Database column types: MySQL’s documented TIMESTAMP range is exactly '1970-01-01 00:00:01' to '2038-01-19 03:14:07' UTC, because internally it stores seconds since the epoch; DATETIME has no such limit but carries no time-zone semantics, so the two are not freely interchangeable. 32-bit time_t: on 64-bit Linux time_t has long been 64-bit, but 32-bit builds, older embedded libc, and any in-house protocol storing time as an int are still on 32. Embedded devices themselves: routers, industrial controllers and smart meters often have lifespans measured in decades, so 2038 is not a distant future for them.
Why is JavaScript unaffected? Because Date holds no 32-bit integer at all — it stores a double-precision millisecond count, with a spec-defined valid range of ±8.64e15 ms, or ±10⁸ days:
new Date(8.64e15).toISOString(); // '+275760-09-13T00:00:00.000Z'
new Date(-8.64e15).toISOString(); // '-271821-04-20T00:00:00.000Z'
new Date(8.64e15 + 1).toString(); // 'Invalid Date'
From roughly 271,821 BC to AD 275,760. 2038 is not a rounding error in that span. The constant 8.64e15 is not arbitrary either: 8.64e15 / 86400000 is exactly 10⁸, so the spec chose “one hundred million days either side of the epoch”.
But — JavaScript has its own 2038 trap, hidden inside a habit almost everyone has written. Date is safe; bitwise operators are not. Every ECMAScript bitwise operator first coerces its operand to a 32-bit integer. So ~~, | 0 and >> 0, all used as “truncate to integer”, are 32-bit truncators:
~~2147483647; // 2147483647 still fine
~~2147483648; // -2147483648 wrapped
2147483648 | 0; // -2147483648
2147483648 >> 0; // -2147483648
~~2200000000; // -2094967296
new Date(~~2200000000 * 1000).toISOString(); // '1903-08-13T16:38:24.000Z'
~~(Date.now() / 1000) is entirely correct until 2038-01-19T03:14:07Z. One second past that, it returns a value denoting 1901-12-13T20:45:52Z, and from then on, as real time advances, the year it reports climbs from 1901 toward 1970. No exception, no NaN — it quietly returns a value wrong by more than 130 years. Use Math.floor() or Math.trunc() instead; they stay on the floating-point path and do no 32-bit truncation. Unsigned right shift >>> 0 extends the range to 2³² − 1, which is 2106-02-07T06:28:15Z — that postpones the problem by 68 years rather than solving it.
Trap four: months start at zero, and nothing complains
The month argument to the Date constructor and to Date.UTC() runs 0 to 11, while the day argument runs 1 to 31. Everyone knows about that inconsistency; what is worth writing down is the shape it takes when combined with timestamp arithmetic.
The classic case is hand-splitting a date string:
const [y, m, d] = '2026-09-10'.split('-').map(Number);
Date.UTC(y, m, d) / 1000; // 1791590400 → 2026-10-10T00:00:00Z wrong
Date.UTC(y, m - 1, d) / 1000; // 1788998400 → 2026-09-10T00:00:00Z right
The two timestamps differ by 2,592,000 seconds — a full 30 days. Note the shape of this error: it produces a perfectly valid timestamp pointing at a real date that is simply one month late. No exception, no Invalid Date, no validation that will catch it.
More insidious is the error introduced by “correcting” it backwards. Someone reads a month out of a timestamp, prints it, sees 8 for September, and fixes it:
const now = new Date(1788998400000); // 2026-09-10T00:00:00Z
now.getUTCMonth(); // 8
Date.UTC(now.getUTCFullYear(), now.getUTCMonth() + 1, 1); // 2026-10-01T00:00:00Z wrong
Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), 1); // 2026-09-01T00:00:00Z right
The 8 returned by getUTCMonth() and the 8 expected by Date.UTC() use the same zero-based numbering. That + 1 turns “start of this month” into “start of next month”, and a monthly report shifts by one month across the board — and since it is wrong every month, there is no anomalous spike to notice.
Two related behaviours are worth memorising. Out-of-range arguments do not error, they silently carry: Date.UTC(2026, 12, 1) gives 2027-01-01T00:00:00Z. And a day argument of 0 means “last day of the previous month”, which collides with zero-based months to produce an accidentally-correct result: Date.UTC(2026, 9, 0) gives 2026-09-30T00:00:00Z, the last day of September. The idiom is useful, but notice why it works — “month 9 = October” and “day 0 = end of previous month” cancel out. Anyone who infers from it that “9 means September” will be wrong on the very next line.
Why these traps “look fine” in the console
Try the examples above and you may well conclude I am making a fuss, because the same Date object hides half the traps under each of two display conventions, and neither mentions getTime(). The same new Date('2026-09-10'), measured:
// TZ=Asia/Shanghai node
const d = new Date('2026-09-10');
console.log(d); // 2026-09-10T00:00:00.000Z
console.log(String(d)); // Thu Sep 10 2026 08:00:00 GMT+0800 (China Standard Time)
console.log(d.getTime()); // 1788998400000
The first line goes through Node’s util.inspect, which prints an ISO string in UTC. You see 2026-09-10T00:00:00.000Z, read it as “midnight on 10 September”, and naturally assume it parsed to midnight in your zone — but that Z says UTC midnight, eight hours from local midnight. The second line goes through toString() (as do string concatenation, template literals and alert), which prints local time. That line is accurate, but your eye lands on Sep 10 first — the date is unchanged, so it looks fine, and the trap is sitting in the 08:00:00 you read as background noise.
Likewise, if you only verify date parsing in UTC+8 or on a UTC server, you will never reproduce the off-by-one-day your users in the Americas are reporting. Not because the code is right, but because your offset makes the two parses look identical: under UTC the offset is zero and '2026-09-10' and '2026-09-10T00:00:00' parse identically; under UTC+8 they differ by eight hours but stay within the same calendar day. To see the date itself shift, the offset has to be negative.
So there is exactly one reliable way to verify time-related behaviour: compare the integers returned by getTime(), never the rendered strings. Equal integers prove the same instant; strings that look the same prove nothing. To reproduce another region’s behaviour, change the zone on the command line — TZ=America/New_York node yourscript.js costs a dozen extra characters and catches an entire class of bug before release.