Explain Codes LogoExplain Codes Logo

How do I get a timestamp in JavaScript?

javascript
performance
timestamp
javascript-timestamp
Nikita BarsukovbyNikita Barsukov·Feb 12, 2025
TLDR

Get the current timestamp in milliseconds since the Unix Epoch with Date.now().

console.log(Date.now()); // Just like Marty McFly, we're time traveling!

Performance-wise precise timing

For situations that need millisecond precision, performance.now() is your guy. 👌 This is particularly helpful in performance benchmarking.

console.log(performance.now()); // Now measured with the precision of a Swiss watch

Obtain Unix timestamp (in seconds)

To fetch the Unix timestamp in seconds, use Math.floor(Date.now() / 1000).

console.log(Math.floor(Date.now() / 1000)); // Unix time, in English: Number of seconds since 1970..., but who's counting? 🤓

More methods to get milliseconds

While Date.now() is generally adequate, there are other techniques if you need clarity or have to deal with older browsers.

  • Syntax wizardry: For quick conversion to milliseconds.
console.log(+new Date()); // Just add a plus, because every little helps!
  • Spelling it out: Clarity counts.
console.log(new Date().getTime()); // Because sometimes, you need to spell it out.
  • Supporting the ancients: When dealing with older IE versions.
if (!Date.now) { Date.now = function() { return new Date().getTime(); }; } // Because ES5 is for cool kids and IE is the awkward cousin at the family dinner 🤷‍♂️

Measure time from page load

Discern the time that has elapsed since the webpage loaded using performance.timing.navigationStart + performance.now().

console.log(performance.timing.navigationStart + performance.now()); // It's been HOW long since this page loaded?😮

Short and sweet Unix timestamp

If brevity and speed matters to you, here's a concise Unix timestamp formulation:

console.log(Date.now() / 1000 | 0); // Nothing to see here, just ninja skills!

Handling time in JavaScript

Working with time in JavaScript might be a bit different from other languages. Here's what you need to know:

  • JavaScript and Unix do not use the same units for timestamps.
  • JavaScript uses milliseconds.
  • Unix, on the other hand, uses seconds.

Advanced techniques and cross-compatibility

Getting a timestamp might be simple, but there are advanced techniques and considerations.

  • jQuery equivalent: Use $.now() to get current millisecond count.
console.log($.now()); // jQuery got your back if JavaScript seems too mainstream. 😎
  • Calculate relative time: Create your own function.
const startTime = Date.now(); const relativeTime = () => Date.now() - startTime; console.log(relativeTime()); // Because who doesn't want their own, personal timestamp? 💁‍♂️
  • Fast and furious: For the coder in a hurry - the bitwise OR |0.
console.log(Date.now() / 1000 | 0); // Only advisable when racing against Vin Diesel 🚗💨
  • Harmony in coding languages: When you need to sync with server time like PHP.
console.log(Math.floor(Date.now() / 1000)); // 🙏 Pray to the gods of sync where milliseconds and seconds harmonize.
  • Always take into consideration the potential risks involved with quick and ingenious methods.