Avatar ·

How to convert Unix timestamp to readable time in JavaScript?

Hi everyone! I'm new to JavaScript and working on a project where I get data from a weather API. The server returns time as a Unix timestamp (e.g., 1717189200). I need to display this time to the user in a readable format, like "12:45" or "12:45:30". I've tried different things, but nothing works.\n\nHere's my code:\n\n```javascript\nlet timestamp = 1717189200;\nlet date = new Date(timestamp);\nconsole.log(date); // Outputs something weird: Thu Jan 01 1970 03:19:49...\n```\n\nWhy am I getting 1970? I thought Unix timestamp is the number of seconds since January 1, 1970. But new Date() expects milliseconds, right? How do I correctly convert the timestamp to normal time? Do I need to consider the user's timezone? I'd appreciate a detailed explanation with code examples. Thanks!