Web

How to Get Unix Timestamp in JavaScript (Date.now())

Learn how to get a Unix timestamp in JavaScript using Date.now() for milliseconds or Math.floor(Date.now() / 1000) for seconds. Covers js get timestamp, conversions, Node.js examples, pitfalls, and helper functions for accurate date and time representation.

1 answer 5 views

How do I get a timestamp in JavaScript?

I want a single number that represents the current date and time, like a Unix timestamp.

Use Date.now() to get a JavaScript timestamp (milliseconds since the Unix epoch); for a Unix timestamp in seconds, divide by 1000 and round down (for example, Math.floor(Date.now() / 1000)). Alternatives that return the same millisecond value are new Date().getTime() or the shorthand +new Date(); for a one-line js get timestamp in seconds use Math.floor(Date.now()/1000).


Contents


Get unix timestamp in JavaScript

The simplest, fastest way to get a timestamp in JavaScript is Date.now(). It returns the number of milliseconds elapsed since the Unix epoch (1970-01-01T00:00:00Z). Example:

javascript
// milliseconds since Unix epoch
const ms = Date.now();
console.log(ms); // e.g. 1704412800000

// unix timestamp in seconds
const secs = Math.floor(ms / 1000);
console.log(secs); // e.g. 1704412800

Two equivalent alternatives:

  • new Date().getTime() — creates a Date object and reads its time value.
  • +new Date() — unary plus coerces the Date to its numeric value (milliseconds).

Date.now() avoids allocating a Date object and is therefore slightly faster. You can read the official docs on Date.now() for syntax and behavior at the Mozilla page: Date.now() - JavaScript | MDN.

If your goal is “one number that represents the current date and time” for APIs or storage, choose seconds (Unix timestamp) or milliseconds consistently and document which you use.


JavaScript timestamp: milliseconds vs seconds

JavaScript’s time is in milliseconds. Unix timestamps used in many systems (and by convention) are in seconds. That mismatch is the source of most confusion.

Why does it matter? Many APIs expect seconds (e.g., 1704412800) while JavaScript Date methods give milliseconds (1704412800000). Use explicit conversion so you don’t accidentally send an extra-zeros value.

Common conversions:

  • Milliseconds to seconds (integer): Math.floor(Date.now() / 1000)
  • Seconds to milliseconds: seconds * 1000

Quick one-liners:

javascript
const unixSeconds = Math.floor(Date.now() / 1000); // common "unix timestamp"
const unixMs = Date.now(); // javascript timestamp (ms)

Want consistent helpers? Keep conversions in small utility functions (examples below) so you won’t mix seconds and milliseconds across codebases.


Browser and Node.js examples (js get timestamp)

Browser (console):

javascript
// milliseconds
console.log(Date.now());

// seconds (Unix timestamp)
console.log(Math.floor(Date.now() / 1000));

Node.js (same API available):

javascript
// milliseconds
console.log(Date.now());

// seconds
console.log(Math.floor(Date.now() / 1000));

Need high-resolution timings for measuring elapsed time (not epoch timestamps)? Use monotonic clocks: in browsers use performance.now() (gives fractional milliseconds since page load), and in Node you can use process.hrtime.bigint() or the performance API from ‘perf_hooks’. Those aren’t epoch-based; they’re for measuring durations (so they won’t give a Unix timestamp). For more examples and patterns for getting a Unix timestamp in JS or Node, see this guide: https://futurestud.io/tutorials/how-to-get-a-unix-timestamp-in-javascript-or-node-js

If you see shorthand code in the wild like +new Date() or new Date().valueOf(), know they return the same millisecond number as Date.now().


Convert Date and Unix timestamp

Convert a specific Date to a unix timestamp (seconds):

javascript
const d = new Date('2026-01-01T00:00:00Z'); // ISO string, UTC
const seconds = Math.floor(d.getTime() / 1000);
console.log(seconds); // e.g. 1704067200

Convert a Unix timestamp (seconds) back to a Date:

javascript
const unixSec = 1704067200;
const date = new Date(unixSec * 1000);
console.log(date.toISOString()); // "2026-01-01T00:00:00.000Z"

Parsing: Date.parse('...') returns milliseconds and is equivalent to new Date(...).getTime(). Always be explicit about timezone when parsing strings—append Z for UTC or use ISO format to avoid ambiguity.

For quick tips and community examples on getting timestamps in JS, the Stack Overflow thread “How do I get a timestamp in JavaScript?” shows common idioms and alternatives: https://stackoverflow.com/questions/221294/how-do-i-get-a-timestamp-in-javascript


Common pitfalls and caveats

  • Seconds vs milliseconds: Always check the API you’re calling. Sending ms when the service expects s (or vice versa) causes off-by-factor-of-1000 bugs.
  • Timezone confusion: Unix timestamps are UTC-based. new Date().toString() shows a local representation, but the numeric timestamp is UTC-based.
  • System clock changes: Date.now() reads the system clock. If the machine’s clock jumps (NTP sync, user change), epoch timestamps will jump too. For measuring elapsed time use monotonic timers (performance.now(), process.hrtime).
  • Browser rounding: Some browsers reduce Date.now() precision to mitigate timing attacks—MDN notes that implementations may round values (for example, Firefox may use ~2 ms precision). See the MDN docs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/now
  • Leap seconds: Most Unix/POSIX timestamps ignore leap seconds; don’t rely on timestamps alone for astronomical accuracy.
  • Unsafe bitwise tricks: Avoid bitwise truncation (e.g., ~~) for timestamps—these can overflow and produce wrong results.

Double-check which unit (s vs ms) your database, API or language binding expects. When in doubt, document the unit and convert at the API boundary.


Helper functions & quick reference

Small, reusable helpers keep code clear:

javascript
// current JS timestamp in ms
function nowMs() {
 return Date.now();
}

// current Unix timestamp in seconds
function nowSecs() {
 return Math.floor(Date.now() / 1000);
}

// convert date input (string/Date/number) to unix seconds
function toUnixSeconds(dateInput = Date.now()) {
 return Math.floor(new Date(dateInput).getTime() / 1000);
}

// convert unix seconds back to Date
function fromUnixSeconds(seconds) {
 return new Date(Number(seconds) * 1000);
}

Quick reference (one-liners):

  • ms: Date.now()
  • seconds: Math.floor(Date.now()/1000)
  • ms from Date: new Date('2026-01-01').getTime()
  • Date from seconds: new Date(seconds * 1000)

These helpers avoid repeating conversions and reduce the chance of mixing units.


Sources


Conclusion

Short answer: use Date.now() for a JavaScript timestamp (milliseconds). For a Unix timestamp (seconds), use Math.floor(Date.now()/1000). Keep conversions in small helper functions, be explicit about seconds vs milliseconds, and prefer monotonic timers for measuring durations rather than using epoch timestamps. With those rules you’ll avoid the common pitfalls when you need a single number that represents the current date and time.

Authors
Verified by moderation
Moderation
How to Get Unix Timestamp in JavaScript (Date.now())