How to get a timestamp in JavaScript: 5 ways with examples
\n\nEvery 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\nIf 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\nWhat is a timestamp and why is it needed?
\n\nA 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
Now let's move on to the most interesting part — how to get it in JavaScript.
\n\nMethod 1: Date.now() — the fastest and most concise
\n\nThis is my favorite method. Date.now() returns the timestamp in milliseconds right now. No extra objects, no constructors — just a number.
// 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\nPros:
\n- \n
- Maximum performance (no object creation) \n
- Readable and short code \n
- Supported in all modern browsers and Node.js \n
Cons:
\n- \n
- Returns only the current moment (cannot set a date) \n
Use Date.now() when you need the current timestamp in JavaScript without any extra effort.
Method 2: new Date().getTime() — a classic
\n\nThis method is familiar to everyone who has worked with JavaScript for more than a year. Create a Date object and call getTime().
// 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\nPros:
\n- \n
- You can get a timestamp for an arbitrary date \n
- Backward compatibility (works in older browsers) \n
- Intuitive syntax \n
Cons:
\n- \n
- Creates an extra object (slightly slower than Date.now()) \n
Choose new Date().getTime() when you need to get a JavaScript timestamp for a specific date or work with different moments in time.
Method 3: +new Date() — the unary plus hack
\n\nFew 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\nPros:
\n- \n
- Very short notation