How to Convert Milliseconds to a Date in JavaScript
A JavaScript timestamp is a number that represents a specific moment in time, measured as the number of milliseconds that have elapsed since the Unix Epoch (00:00:00 UTC on January 1, 1970). A common task is to convert this numerical timestamp back into a human-readable Date object, which can then be formatted for display.
This guide will teach you how to use the Date constructor to convert a timestamp into a Date object and how to format that object for display using the modern and highly recommended Intl.DateTimeFormat API.
The Core Method: The Date Constructor
The Date constructor is the standard, built-in tool for this job. When you pass a single number to it, it interprets that number as a timestamp in milliseconds since the Unix Epoch.
For example, you have a numerical timestamp (e.g., from an API or Date.now()) and you need to convert it into a Date object. How to convert this timestamp into a usable Date object?
// Problem: How to convert this timestamp into a usable Date object?
const timestamp = 1698417000000;
This single, readable line is all you need.
const timestamp = 1698417000000;
// Pass the timestamp directly to the Date constructor.
const dateObject = new Date(timestamp);
console.log(dateObject);
Output (will vary based on your local timezone):
Fri Oct 27 2023 10:30:00 GMT-0400 (Eastern Daylight Time)
The resulting dateObject is a full-fledged Date instance, ready to be used or formatted.
Formatting the Date for Display (Recommended Method)
Once you have a Date object, you almost always want to display it in a human-readable format. While you could build a formatting string manually, it is a strong best practice to use the built-in Intl (Internationalization) APIs, as they are locale-aware and highly configurable.
Using toLocaleString() for Simple Formatting
The date.toLocaleString() method is a quick and easy way to format a date according to the user's locale and timezone.
const date = new Date(1698417000000);
// Default formatting for the user's locale (e.g., US English)
console.log(date.toLocaleString());
// Output: "10/27/2023, 10:30:00 AM"
// You can specify a different locale
console.log(date.toLocaleString('en-GB'));
// Output: "27/10/2023, 10:30:00"
Using Intl.DateTimeFormat for Advanced Formatting
For full control over the output, Intl.DateTimeFormat is the definitive tool. It allows you to specify exactly which components of the date you want to see and how they should be formatted.
const date = new Date(1698417000000);
const options = {
year: 'numeric',
month: 'long',
day: 'numeric',
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
hour12: true,
timeZoneName: 'short',
};
const formatter = new Intl.DateTimeFormat('en-US', options);
const formattedDate = formatter.format(date);
console.log(formattedDate); // Output: "October 27, 2023, 10:30:00 AM EDT"
This method is powerful, flexible, and handles internationalization correctly without manual logic.
The Manual Formatting Method
While not recommended for most use cases, you can always build a formatted string yourself by extracting each component of the date. This gives you absolute control but is also more verbose and not locale-aware.
function formatDateManually(date) {
// Helper to pad numbers with a leading zero
const pad = (num) => num.toString().padStart(2, '0');
const year = date.getFullYear();
const month = pad(date.getMonth() + 1); // getMonth() is zero-based
const day = pad(date.getDate());
const hours = pad(date.getHours());
const minutes = pad(date.getMinutes());
const seconds = pad(date.getSeconds());
return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}
const date = new Date(1698417000000);
console.log(formatDateManually(date)); // Output: "2023-10-27 10:30:00"
This method is useful if you need a very specific, fixed format (like for a database query), but for display purposes, Intl.DateTimeFormat is superior.
Conclusion
Converting a millisecond timestamp to a Date object in JavaScript is a simple and fundamental operation.
- The core of the conversion is the
new Date(timestamp)constructor, which correctly parses the numerical value. - For displaying the resulting
Dateobject, the recommended best practice is to use the built-in internationalization APIs:toLocaleString()for quick formatting andIntl.DateTimeFormatfor full control. - Avoid manual formatting unless you need a specific, non-standard string format for a machine-readable purpose.