How to convert a Unix timestamp to a JavaScript Date object?

To convert a Unix timestamp to a JavaScript Date object, you can use the new Date() constructor and multiply the Unix timestamp by 1000 to convert it from seconds to milliseconds.

Here is an example code snippet:

const unixTimestamp = 1627381868; // Example Unix timestamp // Convert Unix timestamp to JavaScript Date object const dateObj = new Date(unixTimestamp * 1000); console.log(dateObj); // Output: Mon Jul 26 2021 15:51:08 GMT+0530 (India Standard Time)

In the above code, the unixTimestamp variable represents the Unix timestamp you want to convert. By multiplying it with 1000, it converts the timestamp to milliseconds. Then, the resulting value is passed as an argument to the new Date() constructor, which creates a new JavaScript Date object.

The console.log() statement is used to display the converted Date object in the console. The output shows the Date object in the specified format, which includes the day of the week, month, day, year, time, and timezone.