EN
JavaScript - convert unix timestamp to Date
3 points
In this article, we would like to show you how to convert unix timestamp to Date 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.toISOString()); // 2021-09-02T06:30:45.000Z
In this example, we simply use the Date()
constructor and pass timestamp
as an argument to convert unix timestamp to the Date object.
xxxxxxxxxx
1
const getDate = (timestamp) => {
2
return new Date(timestamp * 1000);
3
};
4
5
// Usage example:
6
7
const timestamp = 1630564245; // unix timestamp in seconds
8
const date = getDate(timestamp);
9
10
console.log(date.toISOString()); // 2021-09-02T06:30:45.000Z
Output:
xxxxxxxxxx
1
2021-09-02T06:30:45.000Z