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.ts190 chars4 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