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.

4 min read

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.

javascriptjavascript
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.

javascriptjavascript
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:

javascriptjavascript
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:

javascriptjavascript
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:

javascriptjavascript
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:

javascriptjavascript
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:

javascriptjavascript
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:

javascriptjavascript
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.

javascriptjavascript
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.

javascriptjavascript
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

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.
RunePowered by Rune AI

Frequently Asked Questions

What is a JavaScript timestamp?

A JavaScript timestamp is the number of milliseconds since January 1, 1970, 00:00:00 UTC (the Unix epoch). Date.now() returns the current timestamp. Every Date object stores its value internally as a timestamp.

How do I convert a JavaScript timestamp to seconds?

Divide by 1000 and use Math.floor to drop the decimal: Math.floor(Date.now() / 1000). JavaScript timestamps are in milliseconds, while many other systems and APIs use seconds.

Can I measure how long a function takes to run?

Yes. Call Date.now() before and after the operation, then subtract the first timestamp from the second. For higher precision, use performance.now() which returns sub-millisecond resolution.

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.