JavaScript Date getHours() method - JavaScript Tutorial

JavaScript Date getHours() method

BLUF: This tutorial on JavaScript Date getHours() method provides an in-depth look at JavaScript's core features. It includes practical examples and code snippets to help you master modern JS development.
Key Discovery: JavaScript Date getHours() method

Understanding JavaScript Date getHours() method is crucial for building dynamic, interactive web applications. Explore the examples below to see it in action.

The getHours method in JavaScript retrieves the hour component for a given date according to the local time zone.

Syntax

The syntax for the getHours method is as follows:

Example

dateObj.getHours()

Return

A whole number ranging from 0 to 23 that signifies the hour.

JavaScript Date getHours method example

In this section, we will explore the getHours function by examining several illustrative examples.

Example 1

Let’s examine an example that demonstrates how to display the current hour's value.

Example

<script>

var hour=new Date();

document.writeln(hour.getHours());

</script>

Output:

Example 2

Let’s examine an example that demonstrates how to extract and display the hour from a specified date.

Example

<script>

var hour=new Date("August 15, 1947 20:22:10");

document.writeln(hour.getHours());

</script>

Output:

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience