Skip to main content

How to Get the Current Unix Time in JavaScript

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Math.floor(Date.now() / 1000) is the current Unix time in seconds in JavaScript: the number of seconds since the start of 1 January 1970, UTC. Date.now() on its own counts milliseconds, so it's 1000 times larger.

Syntax​

Syntax
const <seconds> = Math.floor(Date.now() / 1000);
PartWhat it does
Date.now()Returns the current time as the number of milliseconds since 1 January 1970, UTC.
/ 1000Turns milliseconds into seconds, with a fraction left over.
Math.floor(...)Drops the fraction by rounding down, which gives whole seconds.
<seconds>The variable that holds the Unix time.

MDN describes Date.now(): it "returns the number of milliseconds elapsed since the epoch, which is defined as the midnight at the beginning of January 1, 1970, UTC." And Math.floor() "always rounds down and returns the largest integer less than or equal to a given number."

The Example​

The example prints the current time in milliseconds and in seconds, then turns the seconds back into a date with new Date(seconds * 1000) and prints it in ISO 8601 format, to check that the number really is now.

Prerequisites​

  • Node.js. This guide uses Node.js 26.13.1.

Step 1: Write the Code​

Create a file named main.js:

main.js, which stores Date.now() in milliseconds, divides it by 1000 and rounds it down with Math.floor into seconds, and logs both numbers and the seconds turned back into an ISO date.

main.js
const milliseconds = Date.now();
const seconds = Math.floor(milliseconds / 1000);
console.log(milliseconds);
console.log(seconds);
console.log(new Date(seconds * 1000).toISOString());

Step 2: Run It​

Terminal
node main.js
Output
1796059333363
1796059333
2026-11-30T17:22:13.000Z

A zsh terminal where node main.js prints a 13-digit number of milliseconds, a 10-digit number of seconds, and the date 2026-11-30T17:22:13.000Z.

The command is the same in zsh, bash and PowerShell. Your numbers will be later, because they're the time you run it.

What Happened​

  • Date.now() printed 13 digits: the milliseconds since 1970.
  • The seconds have 10 digits. Dividing by 1000 moved the decimal point three places, and Math.floor dropped the fraction, here .363 of a second.
  • The date shows the same moment in UTC. new Date() takes milliseconds, so the seconds are multiplied by 1000 again, and toISOString() always writes UTC: "The timezone is always UTC, as denoted by the suffix Z." The milliseconds are .000 because the fraction was rounded away.

Seconds are what most other systems expect. A JSON Web Token's exp, nbf and iat claims, for example, are Unix times in seconds, and so are the timestamps of most other languages and of the date +%s command.

Common Mistakes​

MistakeWhat happensDo this instead
Using Date.now() where seconds are expectedThe value is 1000 times too large, so a time 15 minutes away looks tens of thousands of years awayDivide by 1000 and round down
Rounding with Math.roundWhen the fraction is .5 or more, it rounds up to a second that hasn't happened yetUse Math.floor
Passing seconds to new Date()new Date() reads milliseconds, so the date lands in January 1970Multiply the seconds by 1000 first

Troubleshooting​

SymptomLikely causeFix
A date in 1970Seconds were passed where milliseconds are expectedMultiply by 1000
A year far in the futureMilliseconds were used where seconds are expectedDivide by 1000 and round down

FAQ​

Is Date.now() in UTC?​

It counts from midnight UTC on 1 January 1970, so the number is the same everywhere in the world at the same moment. Time zones only matter when you turn it into a date you read, and toISOString() always shows UTC.

Why Math.floor and not Math.trunc?​

For a time after 1970 the result is the same, because the number is positive. Math.floor rounds down, which is the usual way to count whole seconds that have passed.

Conclusion​

Math.floor(Date.now() / 1000) gives the current Unix time in seconds. Keep Date.now() for milliseconds, and convert whenever another system expects seconds.

Additional Resources​