How do you get the current Unix timestamp in seconds or milliseconds in JavaScript? | A1Lab@FahadM to JavaScriptEnglish •
edit-2
11 days ago

How do you get the current Unix timestamp in seconds or milliseconds in JavaScript?

message-square
0

Call **Date.now()** to get the current Unix timestamp in **milliseconds**, or divide by 1000 with **Math.floor(Date.now() / 1000)`** to get it in seconds.

Milliseconds vs. Seconds

// 1. Unix timestamp in milliseconds (13 digits)
const timestampMs = Date.now();
console.log(timestampMs); // e.g., 1795108080000

// 2. Unix timestamp in seconds (10 digits - standard for APIs & JWTs)
const timestampSec = Math.floor(Date.now() / 1000);
console.log(timestampSec); // e.g., 1795108080

Why Date.now() Is Preferred

Date.now() returns a primitive number directly without allocating a new Date() object in memory. '