How to get a timestamp in JavaScript: 5 simple ways

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

How to get a timestamp in JavaScript: 5 ways with examples

\n\n

Every developer has at least once faced the task: you need to save a moment in time, compare dates, or send data to a server. And here timestamp in JavaScript comes to the rescue — a universal time marker understood by all systems. In this guide, I will show 5 proven ways to get a timestamp, break down their pros and cons, and answer popular questions from Stack Overflow.

\n\n

If you have ever Googled "how do I get a timestamp in JavaScript" — you are not alone. Thousands of developers search for this answer every day. Let's finally figure out this topic once and for all.

\n\n

What is a timestamp and why is it needed?

\n\n

A timestamp is the number of milliseconds that have passed since January 1, 1970, 00:00:00 UTC. This date is called the Unix epoch. Why is this needed?

\n\n
    \n
  • Universality: a timestamp does not depend on the time zone — it is pure UTC.
  • \n
  • Ease of storage: a single number instead of a date string.
  • \n
  • Comparison: it's easy to calculate the difference between two moments in time.
  • \n
  • Compatibility: timestamps are understood by all programming languages and databases.
  • \n
\n\n

Now let's move on to the most interesting part — how to get it in JavaScript.

\n\n

Method 1: Date.now() — the fastest and most concise

\n\n

This is my favorite method. Date.now() returns the timestamp in milliseconds right now. No extra objects, no constructors — just a number.

\n\n
// Get the current timestamp\nconst currentTimestamp = Date.now();\nconsole.log(currentTimestamp); // 1712345678901\n\n// Can be used immediately for measurements\nconst start = Date.now();\n// ... some code ...\nconst end = Date.now();\nconsole.log(`Execution took ${end - start} ms`);\n
\n\n

Pros:

\n
    \n
  • Maximum performance (no object creation)
  • \n
  • Readable and short code
  • \n
  • Supported in all modern browsers and Node.js
  • \n
\n\n

Cons:

\n
    \n
  • Returns only the current moment (cannot set a date)
  • \n
\n\n

Use Date.now() when you need the current timestamp in JavaScript without any extra effort.

\n\n

Method 2: new Date().getTime() — a classic

\n\n

This method is familiar to everyone who has worked with JavaScript for more than a year. Create a Date object and call getTime().

\n\n
// Get timestamp via Date object\nconst timestamp = new Date().getTime();\nconsole.log(timestamp); // 1712345678901\n\n// You can get a timestamp for a specific date\nconst specificDate = new Date('2024-12-31T23:59:59');\nconst specificTimestamp = specificDate.getTime();\nconsole.log(specificTimestamp); // 1735689599000\n
\n\n

Pros:

\n
    \n
  • You can get a timestamp for an arbitrary date
  • \n
  • Backward compatibility (works in older browsers)
  • \n
  • Intuitive syntax
  • \n
\n\n

Cons:

\n
    \n
  • Creates an extra object (slightly slower than Date.now())
  • \n
\n\n

Choose new Date().getTime() when you need to get a JavaScript timestamp for a specific date or work with different moments in time.

\n\n

Method 3: +new Date() — the unary plus hack

\n\n

Few people know, but the unary plus converts a Date object into a number — its timestamp. This works due to forced type coercion.

\n\n
// Short but not obvious way\nconst timestamp = +new Date();\nconsole.log(timestamp); // 1712345678901\n\n// You can even omit the parentheses\nconst timestamp2 = +new Date;\nconsole.log(timestamp2); // 1712345678902\n
\n\n

Pros:

\n
    \n
  • Very short notation

Blogs

Book Recommendations