×

Index

JavaScript Tutorial

JavaScript Examples

CSS Tutorial

CSS Examples

Date getHours() method with example in JavaScript

JavaScript Date getHours() method: Here, we are going to learn about the getHours() method of Date class with Example in JavaScript.
Submitted by IncludeHelp, on March 04, 2019

JavaScript Date getHours() method

getHours() method is a Date's class method and it is used to get the only hours from current time.

It accepts nothing as the parameter and returns a value between 0 to 23.

Syntax

var dt = new Date();
dt.getHours();

Sample Input/Output

Input:
var dt = new Date();
dt.getHours();
    
Output:
19 (if the time is 19:any_minutes:any_seconds)

JavaScript code to get the hours only from the current time using getHours() method

<html>
<head><title>JavaScipt Example</title></head>

<body>
<script>
	var dt = new Date(); //Date constructor 
	var hours = dt.getHours(); //getting hours from current time
	//printing hour
	document.write("Current hour is : " + hours);
</script>
</body>
</html>

Output

Current hour is : 19

JavaScript Date Object Methods »




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.