How can I get days since epoch in JavaScript?

JavascriptWeb DevelopmentFront End Technology

To get days since epoch, you need to use Math.abs() JavaScript method. Then use the Math.Floor() method to get the difference between dates since epoch and current date −


Live Demo

      <title>JavaScript Clone Date</title>
         var current_date, epocDate;

         current_date = new Date();
         document.write("Current Date: "+current_date);

         var epocDate = new Date(new Date().getTime() / 1000);
         document.write("<br>Since epoch: "+epocDate);
         var res = Math.abs(current_date - epocDate) / 1000;

         // get total days between two dates
         var days = Math.floor(res / 86400);
         document.write("<br>Difference (Days): "+days);


Current Date: Fri May 25 2018 15:42:43 GMT+0530 (India Standard Time)
Since epoch: Sun Jan 18 1970 21:44:03 GMT+0530 (India Standard Time)
Difference (Days): 17658
Updated on 18-Jun-2020 12:07:59