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:
xxxxxxxxxx
1
const timestamp = 1630564245; // unix timestamp in seconds
2
const date = new Date(timestamp * 1000);
3
4
console.log(date); // Thu Sep 02 2021 08:30:45 GMT+0200 (Central European Summer Time)
5
console.log(date.toISOString()); // 2021-09-02T06:30:45.000Z
In this example, we display unix timestamp in human readable form by converting it to the Date
object.
xxxxxxxxxx
1
const timestamp = 1630564245; // unix timestamp in seconds
2
const date = new Date(timestamp * 1000);
3
4
console.log(date); // Thu Sep 02 2021 08:30:45 GMT+0200 (Central European Summer Time)
5
console.log(date.toISOString()); // 2021-09-02T06:30:45.000Z
Output:
xxxxxxxxxx
1
Thu Sep 02 2021 08:30:45 GMT+0200 (Central European Summer Time)
2
2021-09-02T06:30:45.000Z
Note:
We have already explained this solution in detail in the following article:
In this example, we display unix timestamp in human readable form by converting it to the time string.
xxxxxxxxxx
1
const getTime = (timestamp) => {
2
const date = new Date(1000 * timestamp); // unix timestamp in seconds to milliseconds conversion
3
const time = date.toTimeString();
4
const parts = time.split(' ');
5
return parts[0];
6
};
7
8
// Usage example:
9
const timestamp = 1630564245; // unix timestamp in seconds
10
console.log(getTime(timestamp)); // 08:30:45
Output:
xxxxxxxxxx
1
08:30:45
Note:
We have already explained this solution in detail in the following article: