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

Làm thế nào để chuyển đổi epoch Date sang ngày JavaScript có ý nghĩa?


Ngày trong JavaScript mất mili giây kể từ kỷ nguyên. Nếu kỷ nguyên tính bằng mili giây là 1514789449605, thì hãy để chúng tôi chuyển đổi nó thành một ngày JavaScript có ý nghĩa.

Ví dụ

<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>

Đầu ra

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