Computer >> Computer tutorials >  >> Programming >> Javascript

How to convert epoch Date to meaningful JavaScript date?


Dates in JavaScript take milliseconds since epoch. If the epoch in milliseconds is 1514789449605, then let us convert it into a meaningful JavaScript date.

Example

<html>
   <head>
      <title>JavaScript Dates</title>
   </head>
   <body>
      <script>
         var val ="/Date(1514789449605)/";
         var myDate = new Date( parseFloat( val.substr(6 )));
         document.write("Converted Date:"+ (myDate.getMonth() + 1) + "/" +
                      myDate.getDate() + "/" + myDate.getFullYear() + " " +
                      myDate.getHours() + ":" + myDate.getMinutes() + ":" +
                      myDate.getSeconds()
         );
      </script>
   </body>
</html>

Output

Converted Date:1/1/2018 12:20:49