EN
TypeScript - convert unix milliseconds time to Date
0 points
In this article, we would like to show you how to convert unix milliseconds time to Date in TypeScript.
Quick solution:
xxxxxxxxxx
1
const timestamp: number = 1630564245000; // unix timestamp in milliseconds
2
const date: Date = new Date(timestamp);
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 (in milliseconds) to the Date object.
xxxxxxxxxx
1
const getDate = (timestamp: number): Date => {
2
return new Date(timestamp);
3
};
4
5
6
// Usage example:
7
8
const timestamp: number = 1630564245000; // unix timestamp in milliseconds
9
const date: Date = getDate(timestamp);
10
11
console.log(date.toISOString()); // 2021-09-02T06:30:45.000Z
Output:
xxxxxxxxxx
1
2021-09-02T06:30:45.000Z