如何使用當前區域設定慣例將 Date 的“時間”部分作為字串返回?


若要使用當前區域設定慣例將 Date 的“時間”部分作為字串返回,請使用 toLocaleTimeString() 方法。

toLocaleTimeString 方法依賴於底層作業系統來格式化日期。它會使用指令碼執行所在作業系統中的格式化慣例將日期轉換為字串。例如,在美國,月份出現在日期之前(04/15/98),而德國在日期之前顯示月份(15.04.98)。

示例

你可以嘗試執行以下程式碼來將 Date 的“時間”部分作為字串返回 −

<html>
   <head>
      <title>JavaScript toLocaleTimeString Method</title>
   </head>
   <body>
      <script>
         var dt = new Date(2018, 0, 15, 14, 16, 30);
         document.write( "Formated Date - Time : " + dt.toLocaleTimeString() );
      </script>
   </body>
</html>

更新於:2020 年 6 月 23 日

74 次瀏覽

助力開啟 職業生涯

完成課程,獲得認證

開始學習
廣告
© . All rights reserved.