JavaScript Date getUTCSeconds() 方法



getUTCSeconds() 方法是 JavaScript Date 物件原型的一部分。它用於根據協調世界時 (UTC) 檢索日期物件的秒分量。返回值將是一個介於 (0 到 59) 之間的整數,表示日期的秒數。如果提供的 Date 物件“無效”,則此方法返回非數字(NaN)作為結果。此外,此方法不接受任何引數。

UTC 代表協調世界時。它是世界各地調節時鐘和時間的首要時間標準。而印度標準時間 (IST) 是印度採用的時間,IST 和 UTC 之間的時間差為 UTC+5:30(即 5 小時 30 分)。

語法

以下是 JavaScript Date getUTCSeconds() 方法的語法:

getUTCSeconds();

此方法不接受任何引數。

返回值

返回值是一個整數,表示給定日期物件在 UTC 時區中的秒分量。

示例 1

在以下示例中,我們演示了 JavaScript Date getUTCSeconds() 方法的基本用法:

<html>
<body>
<script>
   const currentDate = new Date();
   const seconds = currentDate.getUTCSeconds();

   document.write(seconds);
</script>
</body>
</html>

輸出

它返回根據世界時計算的日期的秒分量。

示例 2

在此示例中,我們檢索並列印提供的日期的秒分量:

<html>
<body>
<script>
   const specificDate = new Date("December 21, 2023 12:30:45 UTC");
   const seconds = specificDate.getUTCSeconds();

   document.write(seconds);
</script>
</body>
</html>

輸出

以上程式返回整數 45 作為秒值。

示例 3

以下示例每 2 秒列印一次根據世界時計算的日期的秒分量。

<html>
<body>
<script>
   function printSeconds() {
      const currentDate = new Date();
      const seconds = currentDate.getUTCSeconds();
      document.write(seconds + "<br>");
   }

   setInterval(printSeconds, 2000);
</script>
</body>
</html>

輸出

如我們所見,輸出每 2 秒列印一次秒數。

廣告