如何藉助當前本地方言慣例將“時間”部分作為字串返回日期?


要藉助當前本地方言慣例將“時間”部分作為字串返回日期,請使用 toLocaleTimeString() 方法。

toLocaleTimeString 方法依賴於底層作業系統對日期的格式設定。它會使用指令碼執行所在作業系統的格式設定慣例將日期轉換成字串。例如,在美國,月份出現在日期前面(04/15/98),但在德國,日期出現在月份前面(15.04.98)。

示例

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

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

更新時間: 23-Jun-2020

74 次瀏覽

開啟你的職業生涯

完成課程,獲得認證

開始
廣告
© . All rights reserved.