Working with Timestamps in JavaScript Guide
Learn how to work with JavaScript timestamps using Date.now and getTime. Practical examples of date arithmetic, time comparisons, and performance timing.
A JavaScript timestamp is a number representing milliseconds since January 1, 1970, 00:00:00 UTC. This moment is called the Unix epoch. Every Date object stores its value as a timestamp internally.
const now = Date.now();
console.log(now);
const date = new Date();
console.log(date.getTime());Both print the same large number, like 1784000000000. Date.now returns the current timestamp directly. getTime extracts the timestamp from an existing Date object.
For an introduction to the Date object itself, see the JavaScript Date object guide.
Getting the current timestamp
Date.now is the simplest way to get the current moment as a number. It requires no Date object and returns milliseconds directly.
const start = Date.now();
for (let i = 0; i < 1_000_000; i += 1) {
// Some work
}
const end = Date.now();
console.log(`Elapsed: ${end - start} ms`);This measures how long the loop took in milliseconds. The difference between two timestamps is always a duration in milliseconds.
Some APIs and databases expect timestamps in seconds rather than milliseconds. Convert by dividing and flooring, using the same Math.floor rounding behavior you would use anywhere else in JavaScript:
const seconds = Math.floor(Date.now() / 1000);
console.log(seconds);This prints the current Unix timestamp in seconds instead of milliseconds, which is the format many server-side APIs expect.
Converting between timestamps and dates
A timestamp is only useful to a person once it becomes a readable date. Passing a millisecond number straight into the Date constructor converts it back into full year, month, and day components:
const timestamp = Date.now();
const date = new Date(timestamp);
console.log(date.getFullYear());
console.log(date.getMonth() + 1);
console.log(date.getDate());Going the other direction, getTime extracts the timestamp from any existing Date object, which is useful when you built a date from year, month, and day values but now need the raw millisecond number:
const date = new Date(2026, 6, 14);
const ts = date.getTime();
console.log(ts);This prints a large millisecond number, the same kind of value Date.now() returns. Once a date is reduced to a timestamp, it can be stored, compared, or transmitted like any other number.
Date arithmetic with timestamps
Timestamps make date arithmetic straightforward because adding and subtracting is just number math. To add days to a date, add milliseconds:
const now = Date.now();
const oneWeekLater = now + 7 * 24 * 60 * 60 * 1000;
const futureDate = new Date(oneWeekLater);
console.log(futureDate);Seven days times 24 hours times 60 minutes times 60 seconds times 1000 milliseconds equals 604800000 milliseconds, exactly one week.
Common duration constants to remember:
const ONE_SECOND = 1000;
const ONE_MINUTE = 60 * 1000;
const ONE_HOUR = 60 * 60 * 1000;
const ONE_DAY = 24 * 60 * 60 * 1000;Use these constants to make your time arithmetic self-documenting instead of leaving unexplained numbers in your code. The same constants are useful for comparing two dates by comparing their timestamps directly:
const deadline = new Date(2026, 11, 31).getTime();
const today = Date.now();
if (today < deadline) {
console.log("Still have time.");
}Measuring elapsed time with performance.now
Date.now is precise to the millisecond. For higher precision, use performance.now which returns sub-millisecond values with microsecond resolution.
const t0 = performance.now();
for (let i = 0; i < 1000; i += 1) {
JSON.parse('{"key":"value"}');
}
const t1 = performance.now();
console.log(`Took ${(t1 - t0).toFixed(3)} ms`);performance.now is designed for performance measurement. Its zero point is relative to the page load, not the Unix epoch, so do not use it for creating dates. It is only useful for measuring durations.
Comparing timestamps across time zones
Timestamps are UTC-based, which means they are time-zone independent. The same moment has the same timestamp regardless of where in the world the code runs.
const now = Date.now();
const localDate = new Date(now);
console.log(localDate.getHours());
const utcHours = new Date(now).getUTCHours();
console.log(utcHours);The first prints your local hour. The second prints the UTC hour. The timestamp is the same, but the displayed components differ based on which get method you call. This means two users in different time zones can share a timestamp and each see the correct local time.
Common mistakes
Confusing seconds and milliseconds is the most frequent timestamp error. The expression new Date(timestampInSeconds) creates a date in the year 1970 because JavaScript expects milliseconds. Always multiply second-based timestamps by 1000 before passing them to Date.
Using Date.now for high-precision timing lacks resolution. Date.now rounds to the nearest millisecond. For sub-millisecond measurements, use performance.now.
Comparing Date objects with === or == does not work as expected. Two different Date objects are never equal even if they represent the same moment, because equality checks object identity, not the stored timestamp. Relational operators like < and > do work correctly on Date objects since JavaScript converts them to timestamps automatically, but converting explicitly with date1.getTime() < date2.getTime() makes the comparison clearer to read.
Rune AI
Key Insights
- A timestamp is milliseconds since Jan 1, 1970 UTC (the Unix epoch).
- Date.now() returns the current timestamp. Date.getTime() returns a stored date's timestamp.
- Subtract timestamps to measure elapsed time in milliseconds.
- Divide by 1000 for seconds, 60000 for minutes, 86400000 for days.
- Use performance.now() for sub-millisecond precision timing.
Frequently Asked Questions
What is a JavaScript timestamp?
How do I convert a JavaScript timestamp to seconds?
Can I measure how long a function takes to run?
Conclusion
Timestamps are the foundation of date and time work in JavaScript. Use Date.now for the current moment as milliseconds, getTime to extract a timestamp from an existing Date, and subtract timestamps to measure durations or compare moments. For high-precision timing, use performance.now instead of Date.now.
More in this topic
JavaScript While Loop Explained: A Complete Guide
A while loop repeats code as long as a condition stays true. Learn the syntax, see practical examples, and understand when a while loop is the right choice over a for loop.
JavaScript Loops Tutorial: for, while, do while
Loops let JavaScript repeat code without writing it twice. Learn the three core loop types, what each one does, and when to choose one over another.
How to Loop Through Arrays Using JS for Loops Guide
Looping through arrays is the most practical use of JavaScript for loops. Learn how to access each element by index, avoid off-by-one errors, and choose between for, for...of, and forEach.