How to convert a Unix timestamp to time in JavaScript?


To convert a Unix timestamp to time, you can try to run the following code in JavaScript −

Example

Live Demo

<html>
   <head>
      <title>JavaScript Dates</title>
   </head>
   <body>
      <script>
         var unix_time = 1514791901 ;
         var date = new Date(unix_time*1000);

         // get hours
         var hrs = date.getHours();

         // get minutes
         var min = "0" + date.getMinutes();

         // get seconds
         var sec = "0" + date.getSeconds();

         document.write("Time- "+hrs + ":" + min.substr(-2) + ":" + sec.substr(-2));
      </script>
   </body>
</html>

Output

Time- 13:01:41

Updated on: 18-Jun-2020

150 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements