You are on page 1of 2

Write HTML code to design a form that displays two buttons START and

STOP. Write a JavaScript code such that when user clicks on START button,
real time digital clock will be displayed on screen. When user clicks on STOP
button, clock will stop displaying time. (Use Timer methods)
<html>
<head>
<title>Digital Clock</title>
<button onclick="start()">START</button>
<button onclick="stop()">STOP</button>
</head>
<body>
<div id="clock"></div>
<script>
var t;
function updateClock() {
let date = new Date();
let hours = date.getHours();
let minutes = date.getMinutes();
let seconds = date.getSeconds();
let meridiem = "AM";
if (hours > 12) {
hours -= 12;
meridiem = "PM";
}
if (hours === 0)
hours = 12;
if (minutes < 10)
minutes = "0" + minutes;
if (seconds < 10)
seconds = "0" + seconds;

let time = hours + ":" + minutes + ":" + seconds;


document.getElementById("clock").innerHTML = hours + ":" + minutes + ":" + seconds;
}
function start()
{
t=setInterval(updateClock, 1000);
}

function stop()
{
clearInterval(t);
}
</script>
</body>
</html>

You might also like