To convert a Unix timestamp to a date in JavaScript, we can use the Date()
constructor and pass in the timestamp as an argument. The constructor takes the number of milliseconds since January 1, 1970, so we need to multiply the timestamp by 1000. We can then use various methods of the Date()
object to get different parts of the date.
index.tsx660 chars19 lines
In the above code block, we first define a Unix timestamp (in seconds). We then multiply this by 1000 to convert it to milliseconds and pass it as an argument to the Date()
constructor to create a new date object. We can then use various methods of the Date()
object to get the different parts (year, month, day, etc.) of the date. Finally, we format the date as a string using template literals and output it to the console.
gistlibby LogSnag