The JavaScript getHours function is one of the date methods that returns the hours value ranging from 0 to 23 for the given date according to local time.
Syntax
The syntax of the JavaScript getHours() function is
dateObject.getHours()
Return Value: The getHours() method returns an integer between 0 and 23 representing the hours of the given date according to the locale’s time. If the date object is invalid, it returns NaN.
JavaScript getHours Example
The following examples help understand how the getHours() method extracts and returns the hours in 24-hour format.
Example 1: In this example, we use the getHours() function to extract and return the hours value from the current date and time.
const dt = new Date();
const h = dt.getHours();
console.log(h);
11
Example 2: In this example, we use the getHours() method to extract the hours from the custom date and time.
const dt = new Date("April 29, 2022 12:09:07");
console.log(dt.getHours());
12
Example 3: This JavaScript example extracts the hours from a custom date without hours. If we don’t pass a time value, the date() constructor adds a default 00:00:00 as the time value. So, the JavaScript getHours() method will return 0 hours.
const dt = new Date("April 10, 2022");
console.log(dt.getHours());
0
Example 4: If we use the getHours() function on an invalid date (wrong day or month number), it returns NaN. For example, the code below returns NaN because we are using 40 as the day number.
const dt = new Date("November 40, 2022 10:11:12");
console.log(dt.getHours());
NaN
Example 5: We can use the getHours() function to extract the hour and print a message on the webpage. For instance, we can use the else if statement and the getHours() to display a Good morning message to web users.
function greetings()
{
const h = new Date().getHours();
if (h >= 6 && h < 12) {
return "Good Morning!";
}
else if (h >= 12 && h < 18) {
return "Good Afternoon!";
}
else if (h >= 18 && h < 21) {
return 'Good Evening!';
}
else {
return "Good Night....";
}
}
console.log(greetings());
Good Morning!
How do I get the current hour and minute in JS?
We must use the combination of the getHour() function to get the current hour and the getMinutes() method to get the minutes.
const dt = new Date();
let h = dt.getHours();
console.log(h)
let m = dt.getMinutes();
console.log(m)
console.log(`${h}:${m}`);
9
28
9:28
JavaScript getHours in 12-hour format
By default, the getHour() function returns Hours in 24-hour format. To print in 12-hour format, we must use the following example.
const dt = new Date('April 29, 2022 15:09:07');
const h = dt.getHours();
const h12 = h % 12 || 12;
const e = h >= 12 ? 'PM' : 'AM';
console.log(`${h12} ${e}`);
3 PM
Apart from the above mentioned manual approach, we can use the below shown modern approach of toLocalString to achieve the same.
const dt = new Date('April 29, 2022 15:09:07');
let h = dt.toLocaleString('en-US', { hour: 'numeric', hour12: true });
console.log(h);
3 PM
JavaScript getHours 2 digits
By default, the getHours() function returns a single-digit value for the first 9 hours. For uniformity, we can convert the integer value to a string and use the padStart() function to add leading zeros to the hours.
const dt = new Date();
let h = dt.getHours();
const hours = String(h).padStart(2, '0');
console.log(h);
console.log(hours);
9
09