How to convert a Unix timestamp to a date
Short answer
A Unix timestamp counts seconds, and JavaScript dates count milliseconds. Multiply by 1000:
const timestamp = 1735689600;
const date = new Date(timestamp * 1000);
console.log(date.toISOString());Output:
2025-01-01T00:00:00.000ZIn a readable form
Show the date in a specific time zone with the timeZone option:
const date = new Date(1735689600 * 1000);
console.log(date.toLocaleString('en-GB', { timeZone: 'UTC' }));
console.log(date.toLocaleString('en-US', { timeZone: 'America/New_York' }));Output:
01/01/2025, 00:00:00
12/31/2024, 7:00:00 PMFrom a date to a timestamp
getTime() returns milliseconds. Divide by 1000 and round down to get seconds:
const date = new Date(Date.UTC(2025, 0, 1));
console.log(Math.floor(date.getTime() / 1000));Output:
1735689600Common mistake
If you forget to multiply by 1000, you get a date in January 1970:
console.log(new Date(1735689600).toISOString());Output:
1970-01-21T02:08:09.600ZFunctions used in this recipe
- Date.now — Returns the current time as a number of milliseconds.
- Date.prototype.toISOString — Returns the date in ISO 8601 format in UTC.
- Date.prototype.getTime — Returns the date as a number of milliseconds.