How to get current timestamp in JavaScript
Getting the current timestamp is essential for logging, performance measurement, and unique ID generation in JavaScript applications.
As the creator of CoreUI with over 25 years of development experience, I’ve used timestamps extensively in production systems for tracking user interactions and measuring component performance.
The most efficient and widely supported method is using the Date.now()
static method.
This approach provides milliseconds since Unix epoch with minimal overhead and maximum browser compatibility.
Use Date.now()
to get the current timestamp as milliseconds since Unix epoch.
const timestamp = Date.now()
The Date.now()
method returns the current timestamp as a number representing milliseconds elapsed since January 1, 1970 UTC. This static method doesn’t create a Date object, making it the fastest way to get the current time. The returned value can be used for calculations, logging, or converted back to a Date object when needed.
Best Practice Note:
This is the same approach we use in CoreUI components for performance monitoring and cache invalidation.
Avoid new Date().getTime()
when you only need the timestamp, as Date.now()
is significantly faster and doesn’t create unnecessary objects.