EN
JavaScript - convert unix timestamp to human readable
0
points
In this article, we would like to show you how to convert unix timestamp to human readable in JavaScript.
Quick solution:
// ONLINE-RUNNER:browser;
const timestamp = 1630564245; // unix timestamp in seconds
const date = new Date(timestamp * 1000);
console.log(date); // Thu Sep 02 2021 08:30:45 GMT+0200 (Central European Summer Time)
console.log(date.toISOString()); // 2021-09-02T06:30:45.000Z
1. Convert unix timestamp to Date
object
In this example, we display unix timestamp in human readable form by converting it to the Date
object.
// ONLINE-RUNNER:browser;
const timestamp = 1630564245; // unix timestamp in seconds
const date = new Date(timestamp * 1000);
console.log(date); // Thu Sep 02 2021 08:30:45 GMT+0200 (Central European Summer Time)
console.log(date.toISOString()); // 2021-09-02T06:30:45.000Z
Output:
Thu Sep 02 2021 08:30:45 GMT+0200 (Central European Summer Time)
2021-09-02T06:30:45.000Z
Note:
We have already explained this solution in detail in the following article:
2. Convert unix timestamp to time
In this example, we display unix timestamp in human readable form by converting it to the time string.
// ONLINE-RUNNER:browser;
const getTime = (timestamp) => {
const date = new Date(1000 * timestamp); // unix timestamp in seconds to milliseconds conversion
const time = date.toTimeString();
const parts = time.split(' ');
return parts[0];
};
// Usage example:
const timestamp = 1630564245; // unix timestamp in seconds
console.log(getTime(timestamp)); // 08:30:45
Output:
08:30:45
Note:
We have already explained this solution in detail in the following article: