Computer >> Máy Tính >  >> Lập trình >> Javascript

Làm thế nào tôi có thể nhận được số ngày kể từ kỷ nguyên trong JavaScript?


Để biết số ngày kể từ kỷ nguyên, bạn cần sử dụng phương thức JavaScript Math.abs (). Sau đó, sử dụng phương thức Math.Floor () để nhận được sự khác biệt giữa các ngày kể từ kỷ nguyên và ngày hiện tại -

Ví dụ

<html>
   <head>
      <title>JavaScript Clone Date</title>
   </head>
   <body>
      <script>
         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);
      </script>
   </body>
</html>

Đầu ra

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