Unix Timestamp in JavaScript
In JavaScript, Date.now() returns the current time in milliseconds since the Unix epoch. Divide by 1000 for the seconds value most backends expect, and use new Date(ms) to turn a timestamp back into a date.
Get & convert epoch time in JavaScript
const d = new Date(1700000000000); console.log(d.toISOString()); // 2023-11-14T22:13:20.000Z Math.floor(Date.now() / 1000); // current epoch
Gotcha: Date works in milliseconds, not seconds — forgetting the ÷1000 is the most common bug when talking to APIs that use Unix seconds.
Frequently asked questions
- How do I get the current Unix timestamp in JavaScript?
- Math.floor(Date.now() / 1000) gives seconds; Date.now() gives milliseconds.
- How do I convert a Unix timestamp to a Date in JavaScript?
- new Date(seconds * 1000) — multiply by 1000 because the Date constructor expects milliseconds.