How to Get the Date String from an Epoch in JavaScript


Suppose I just wanted the date string from an epoch.

In JavaScript, the Date object returns this:

const epochInMilliseconds = 1602647518000;
const date = new Date(epochInMilliseconds);
// Tue Oct 13 2020 22:51:58 GMT-0500 (Central Daylight Time)

We can get a stringified version of this Date object with the toLocaleString() method.

dateString = date.toLocaleString();
// 10/13/2020, 10:51:58 PM

To get just the date in that Date object, we can use the toLocaleDateString() method.

dateString = date.toLocaleDateString();
// 10/13/2020

The following method is also available if we need just the local time.

timeString = date.toLocaleTimeString();
// 10:51:58 PM

If you need to convert to an epoch from a date, you can check out how to do that in this article.