Encodera

Thanks for reading!

Donate to the site — we work hard for you.

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.000Z

In 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 PM

From 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:

1735689600

Common 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.600Z

Functions used in this recipe

Thanks for reading!

Donate to the site — we work hard for you.