Start your developer blog today!
Email me

How to Get the Date String from an Epoch in JavaScript

Oct 13, 2020 1 min read

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