A timestamp represents a specific moment in time as a number, typically the count of milliseconds elapsed since the Unix Epoch (January 1, 1970, 00:00:00 UTC). JavaScript’s built-in Date object offers several ways to generate one, each suited to slightly different use cases like logging, performance tracking, or storing event times.
Using Date.now()
The most direct and efficient way to get the current timestamp is Date.now(). It’s a static method, meaning it doesn’t require creating a Date object instance first, which makes it the fastest option for simply grabbing the current moment.
const timestamp = Date.now();
console.log(timestamp); // e.g., 1754899200000
This returns the number of milliseconds since the epoch and is the recommended approach for most modern JavaScript code.
Using getTime()
If you already have a Date object, the getTime() method returns the same millisecond value as Date.now().
const now = new Date();
const timestamp = now.getTime();
console.log(timestamp);
This method is especially useful when comparing two dates or calculating durations, since subtracting one getTime() value from another gives the difference in milliseconds.
Using valueOf()
The valueOf() method returns the primitive numeric value of a Date object, which is identical to what getTime() produces.
const timestamp = new Date().valueOf();
console.log(timestamp);
It’s functionally the same as getTime(), just called differently.
Using the Unary Plus Operator
Applying the unary + operator to a Date object forces JavaScript to coerce it into a number, producing the same millisecond timestamp.
const timestamp = +new Date();
console.log(timestamp);
This works but is generally considered less readable than Date.now(), so it’s best reserved for cases where brevity matters more than clarity.
Getting a Timestamp in Seconds
Some systems, particularly Unix-based tools and certain APIs, expect timestamps in seconds rather than milliseconds. To convert, divide by 1000 and round down.
const seconds = Math.floor(Date.now() / 1000);
console.log(seconds);
Comparing Two Dates
Since timestamps are just numbers, comparing dates or measuring elapsed time becomes straightforward:
const start = new Date('2026-01-01').getTime();
const end = new Date('2026-06-01').getTime();
const differenceInDays = Math.round((end - start) / (1000 * 60 * 60 * 24));
console.log(differenceInDays);
This pattern is common for measuring how long a piece of code takes to run or how much time has passed between two events.
Which Method Should You Use?
For most everyday needs, Date.now() is the best choice since it’s the fastest and most readable option. Reach for getTime() when you’re already working with an existing Date object, and use the seconds conversion when integrating with APIs or systems that expect Unix time in seconds rather than milliseconds.
Join The Discussion
Timestamps show up everywhere in JavaScript development, from logging and analytics to scheduling and database records. Have you run into tricky timezone issues while working with timestamps, or found a clever use case for them in your own projects? Whether you’re just learning the Date object or you’ve been juggling Unix time for years, share your tips, questions, or war stories below — what’s your go-to method, and why?