如何在 JavaScript 中將當前時間設定為其他時間?


由於 JavaScript 使用系統時間,該時間顯示當前日期,因此無法使用 JavaScript 進行此操作。然而,你可以透過更改時區來更改當前日期,如下面的程式碼所示 -

示例

 活動示例

<!DOCTYPE html>
<html>
   <body>
      <script>
         var date, offset, nd;
         date = new Date();
       
         document.write("Current: "+date);
         utc = date.getTime() + (date.getTimezoneOffset() * 60000);
       
         // Singapore is GMT+8
         offset = 8;
       
         nd = new Date(utc + (3600000*offset));
         document.write("<br>Singapore Time: "+nd.toLocaleString());
      </script>
   </body>
</html>

輸出

更新於:2020-06-23

458 次瀏覽

開啟你的職業生涯

完成課程以獲得認證

馬上開始
廣告
© . All rights reserved.