Home »
JavaScript Examples
Get current time in Hours24: Minutes: Seconds format in JavaScript
JavaScript getting the current time in HH24:MM:SS format: Here, we are going to learn how to get the current time in JavaScript in Hours24 : Minutes : Seconds format?
Submitted by IncludeHelp, on March 04, 2019
Getting current time in JavaScript
To get the current time in JavaScript, we need to use three library functions of Date class,
- Date getHours() Method – It returns current hours
- Date getMinutes() Method – It returns current minutes
- Date getSeconds() Method – It returns current seconds
Note: To call these functions, we need to create an object to the Date class using Date class constructor.
Examples:
Date class constructor:
var dt = new Date();
Function calls:
dt.getHours();
dt.getMinues();
dt.getSeconds();
Output:
20
12
35
JavaScript code to get the current time in Hours24: Minutes: Seconds format
<html>
<head><title>JavaScipt Example</title></head>
<body>
<script>
var dt = new Date(); //Date constructor
var hh = dt.getHours();
var mm = dt.getMinutes();
var ss = dt.getSeconds();
//printing time
document.write("Current time is= " + hh + ":" + mm + ":" + ss + "<br>");
</script>
</body>
</html>
Output
Current time is= 20:12:35
JavaScript Examples »