How to convert a Unix timestamp to time in JavaScript?

JavascriptWeb DevelopmentFront End Technology

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
raja
Published on 14-Feb-2018 12:14:45
Advertisements