Course contentsShow
← JavaScript
Lesson 1615 of 2,72335. Standard Library and Built-insFree lesson

Date.now for Current Timestamp

Call Date.now() to get current milliseconds since epoch without creating a Date object, useful for performance timing.

Date.now for Current Timestamp

You'll learn how to get the current timestamp in milliseconds without creating a Date object, perfect for timing and performance measurements.

What Is Date.now()?

Date.now() is a static method that returns the number of milliseconds that have elapsed since January 1, 1970, 00:00:00 UTC (the Unix epoch). Unlike the Date constructor, it doesn't create a Date object at all—it just gives you a raw number representing "right now."

Think of it like checking a stopwatch that's been running since 1970. Instead of getting a full clock display with hours, minutes, and seconds, you just get the total millisecond count.

Why Use Date.now()?

There are two main advantages:

  1. Performance: Creating Date objects takes time and memory. Date.now() is faster and lighter.
  2. Simplicity: When you only need a timestamp (not a formatted date), this gives you exactly what you need.

Common Use Case: Timing Code

const start = Date.now();

// Some operation you want to time
for (let i = 0; i < 1000000; i++) {
  Math.sqrt(i);
}

const end = Date.now();
const duration = end - start;

console.log(`Operation took ${duration} milliseconds`);

Both start and end are just numbers, making subtraction simple and efficient.

Comparison with new Date()

These two approaches get you the same timestamp, but with different performance:

const timestamp1 = Date.now();           // Fast, returns number directly
const timestamp2 = new Date().getTime(); // Slower, creates object first

Key Takeaway: Use Date.now() when you need the current timestamp as a number—it's the fastest way to capture "now" for performance timing, logging, or timestamp comparisons.