Why your JavaScript Unix timestamp shows a date in 1970
Decode the same Unix timestamp as seconds and milliseconds, verify the UTC result, and handle zero, negative values and numeric strings in JavaScript.
- Author
- Salon Rai
- 3 min read
If new Date(1704067200) gives you January 1970, check the timestamp unit. The number represents January 1, 2024 when expressed in Unix seconds. JavaScript’s numeric Date constructor reads it as milliseconds. Multiply seconds by 1,000 once at the boundary where the value enters your application.
Decode the same number two ways
A Unix timestamp measures elapsed time from January 1, 1970 at midnight in Coordinated Universal Time (UTC). One second contains 1,000 milliseconds. Passing a value in seconds to a constructor expecting milliseconds places the date 1,000 times closer to the epoch than intended. MDN JavaScript Date reference
const createdAtSeconds = 1704067200;
console.log(new Date(createdAtSeconds).toISOString());
// 1970-01-20T17:21:07.200Z
console.log(new Date(createdAtSeconds * 1000).toISOString());
// 2024-01-01T00:00:00.000Z
Paste 1704067200 into the Unix timestamp converter. Select Seconds, note the UTC output, then select Milliseconds. Changing the unit changes the instant. Switching a display from UTC to local time only changes how the same instant is written. A time zone difference can cross midnight, but can’t account for a date decades away.
Check the producer before changing the parser
Look at the field in the raw response, then read the producer’s contract. A field named created_at tells you its meaning but says little about its unit. Some producers return an ISO date string, some send integer seconds, and others send milliseconds. JavaScript’s Date.now() already returns milliseconds. Multiplying that value again yields a date far in the future if it is still within the supported range.
A ten-digit number is a helpful clue for a modern timestamp in seconds. It is an unreliable validation rule. For example, 86400000 means one day after the epoch if the producer sends milliseconds. The same value in seconds is 1,000 days after the epoch. Both dates are plausible for historical data. If you control the contract, name the field createdAtSeconds or createdAtMs and test it with a known UTC date.
Normalize an integer timestamp once
This function accepts integer numbers or integer strings and requires the unit from the caller. That contract deliberately excludes fractions. If your producer sends decimal seconds, specify the precision and rounding rules for that field before using it in comparisons or storage.
function readTimestamp(value, unit) {
if (unit !== 's' && unit !== 'ms') {
throw new TypeError('Expected unit s or ms');
}
if (typeof value === 'string' && /^-?\d+$/.test(value.trim())) {
value = Number(value.trim());
}
if (!Number.isSafeInteger(value)) {
throw new TypeError('Expected an integer timestamp');
}
const date = new Date(unit === 's' ? value * 1000 : value);
if (Number.isNaN(date.getTime())) throw new RangeError('Date out of range');
return date;
}
console.log(readTimestamp('1704067200', 's').toISOString());
console.log(readTimestamp(0, 'ms').toISOString()); // the epoch
console.log(readTimestamp(-1, 'ms').toISOString()); // just before the epoch
The explicit type check matters. A string passed directly to new Date(...) takes the date-string parsing path. Converting an empty string or null with Number(...) can turn missing data into a zero, producing January 1, 1970 even when there was no timestamp. The function rejects those inputs. It also keeps zero, which is a valid instant that a truthiness check would discard.
Distinguish an epoch default from a unit mismatch
The exact date displayed is a useful clue. January 20, 1970 is consistent with the seconds-as-milliseconds example above. January 1, 1970 at exactly midnight UTC points to a numeric zero. Trace a zero back through defaults, database fields and form handling before multiplying anything. Multiplying zero by 1,000 still produces zero.
Suppose an order history contains three responses. Each of these requires a different parsing decision.
| Raw field in the response | Contract | Expected behavior |
|---|---|---|
{"created_at":1704067200} |
Integer seconds | Multiply once at the boundary. |
{"created_at":"2024-01-01T00:00:00Z"} |
ISO string with a UTC designator | Parse the supplied instant directly. |
{"created_at":null} |
Optional timestamp | Render the missing state, such as “Pending.” |
A conversion applied to every field named created_at would break at least one of those cases. If multiple endpoints feed one view, give each endpoint a small adapter. Have the view consume a single documented representation, such as milliseconds since the epoch or a missing value. The formatter can then focus on how to display the date.
Preserve precision on round trips
If the destination accepts only whole seconds, converting from milliseconds discards information. For 1704067200456 milliseconds, flooring the value divided by 1,000 gives 1704067200 seconds. Multiplying back gives 1704067200000. The missing 456 milliseconds can matter if several events happen within the same second. Store the original precision when event ordering depends on it.
Check your system’s convention for instants before the epoch. For -1 millisecond, flooring the quotient gives -1 second, while truncating toward zero gives 0 seconds. Use the rule expected by the destination and include a negative fixture if your data includes historical dates. A test using only today’s timestamp would miss that difference.
Next steps
Keep one value from a failing response and the instant its producer says it represents. Assert the ISO output in a test at the parsing boundary. Store or pass the normalized value in one unit throughout the rest of the application. If that test passes but a rendered page still changes day after load, compare the two display zones using the React date hydration example.

Written by