JavaScript getHours() | Get the Hour (0-23)

The JavaScript getHours() method is used to get the hour (0-23). For example:

HTML with JavaScript Code
<!DOCTYPE html>
<html>
<body>
   
   <p id="xyz"></p>

   <script>
      const d = new Date();
      let hour = d.getHours();
      document.getElementById("xyz").innerHTML = hour;
   </script>

</body>
</html>
Output

JavaScript getHours() Syntax

The syntax of getHours() method in JavaScript is:

x.getHours()

The x must be an object of the Date() constructor.

The getHours() method returns a number from 0 to 23 which will be an hour of the local time.

Please note: To display date in format dd-mm-yyyy, refer to its separate example.

Please note: To display time in format hh:mm:ss, refer to its separate example.

Please note: To display time in format hh:mm:ss AM/PM, refer to its separate example.

JavaScript Online Test


« Previous Tutorial Next Tutorial »



Like/Share Us on Facebook 😋