Working with dates and timestamps in JavaScript can be slightly confusing because JS handles time in milliseconds, whereas standard Unix timestamps use seconds.
Here is a quick reference guide to handle both correctly.
- Get Current Timestamp in Milliseconds By default, JavaScript's Date.now() returns the current timestamp in milliseconds:const timestampMs = Date.now(); console.log(timestampMs); // Output: 1726053000000
- Get Current Timestamp in Seconds (Standard Unix) To convert it to standard Unix seconds, divide by 1000 and use Math.floor():const timestampSec = Math.floor(Date.now() / 1000); console.log(timestampSec); // Output: 1726053000
- Convert Unix Timestamp Back to Date If you have a Unix timestamp in seconds, multiply it by 1000 before passing it to new Date():const unixTimestamp = 1726053000; const date = new Date(unixTimestamp * 1000); console.log(date.toISOString()); Need a quick online tool to convert timestamps or test timezones? I built a fast, zero-bloat web tool to convert dates, timestamps, and timezones instantly: 👉 timestampeasy.com
Top comments (0)