JavaScript Date getMinutes() 方法



JavaScript 中的 Date.getMinutes() 方法用於檢索日期物件的分元件,表示小時的分鐘數,是一個介於 0 和 59 之間的數字。它根據系統本地時區提取日期物件的分部分。

如果未提供日期作為引數,則 getMinutes() 方法將返回其呼叫的 Date 物件的分元件,基於本地時間。如果 Date 物件無效,則此方法返回 NaN 作為結果。

語法

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

getMinutes();

此方法不接受任何引數。

返回值

此方法返回一個整數,表示給定 Date 物件的分元件,範圍從 0 到 59。

示例 1

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

<html>
<body>
<script>
   const currentDate = new Date();
   const minutes = currentDate.getMinutes();

   document.write("Current Minutes:", minutes);
</script>
</body>
</html>

輸出

以上程式根據本地時間返回日期的分鐘數。

示例 2

在這裡,我們使用 getMinutes() 方法從提供的日期中獲取分元件。

<html>
<body>
<script>
   const specificDate = new Date('2023-12-25 12:45:30');
   const minutes = specificDate.getMinutes();

   document.write("Minutes of provided Date:", minutes);
</script>
</body>
</html>

輸出

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

廣告