convert a unix timestamp to a date in typescript

To convert a Unix timestamp to a date in TypeScript, you can use the Date constructor and the getTime() method.

Here's an example code snippet:

index.ts
const unixTimestamp = 1610543440;
const date = new Date(unixTimestamp * 1000); // multiply by 1000 to convert to milliseconds
console.log(date.toDateString()); // outputs: "Sun Jan 10 2021"
190 chars
4 lines

In the code above, we first declare the Unix timestamp (in seconds) that we want to convert. We then create a new Date object with the timestamp passed as an argument, multiplied by 1000 to convert to milliseconds. Finally, we use the toDateString() method to output the date in a human-readable format.

Note that the toDateString() method returns the date in the local time zone of the browser or Node.js environment. If you want to format the date in a specific time zone, you can use a library like Moment.js or date-fns.

gistlibby LogSnag