EN
TypeScript - convert unix timestamp to Date
0
points
In this article, we would like to show you how to convert unix timestamp to Date in TypeScript.
Quick solution:
const timestamp: number = 1630564245; // unix timestamp in seconds
const date: Date = new Date(timestamp * 1000);
console.log(date.toISOString()); // 2021-09-02T06:30:45.000Z
Practical example
In this example, we simply use the Date() constructor and pass timestamp as an argument to convert unix timestamp to the Date object.
const getDate = (timestamp: number): Date => {
return new Date(timestamp * 1000);
};
// Usage example:
const timestamp: number = 1630564245; // unix timestamp in seconds
const date: Date = getDate(timestamp);
console.log(date.toISOString()); // 2021-09-02T06:30:45.000Z
Output:
2021-09-02T06:30:45.000Z