Using getHours(), getMinutes(), and getSeconds() to display the time: - Javascript Date

Javascript examples for Date:getHours

Description

Using getHours(), getMinutes(), and getSeconds() to display the time:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<button onclick="myFunction()">Test</button>

<p id="demo"></p>

<script>
function addZero(i) {
    if (i < 10) {
        i = "0" + i;
    }//  w ww .  ja v a2  s.com
    return i;
}

function myFunction() {
    var d = new Date();
    var x = document.getElementById("demo");
    var h = addZero(d.getHours());
    var m = addZero(d.getMinutes());
    var s = addZero(d.getSeconds());
    x.innerHTML = h + ":" + m + ":" + s;
}
</script>

</body>
</html>

Related Tutorials