How to convert a timestamp to a date in JavaScript

Converting timestamps to readable dates is fundamental when working with APIs, databases, and time-based data in web applications. As the creator of CoreUI, a widely used open-source UI library, I’ve implemented timestamp conversion in countless dashboard components, data tables, and real-time monitoring systems. From my expertise, the most straightforward and reliable solution is to pass the timestamp directly to the new Date() constructor. This approach works with both Unix timestamps (seconds) and JavaScript timestamps (milliseconds) with proper conversion.

Pass the timestamp to the new Date() constructor to convert it to a date object.

const timestamp = 1672531200000
const date = new Date(timestamp)

The new Date() constructor accepts a timestamp parameter representing milliseconds since January 1, 1970 UTC. If you have a Unix timestamp (seconds since epoch), multiply by 1000 to convert to milliseconds: new Date(unixTimestamp * 1000). The resulting Date object can then use all standard date methods like toLocaleDateString(), getFullYear(), or toISOString() for formatting and manipulation. This approach automatically handles time zones and provides accurate date representation.

Best Practice Note:

This is the same approach we use in CoreUI components for displaying API timestamps and log entries. Always verify whether your timestamp is in seconds or milliseconds - API responses commonly use Unix timestamps (seconds), while JavaScript internally uses milliseconds.


Speed up your responsive apps and websites with fully-featured, ready-to-use open-source admin panel templates—free to use and built for efficiency.


About the Author