Digital Clock

<!DOCTYPE html>
<html>
<head>
  <title>Digital Clock</title>
</head>
<body>

  <!-- Heading to display time -->
  <h1 id="clock"></h1>

  <!-- Link JavaScript file -->
  <script src="script.js"></script>

</body>
</html>
// Function to display current time
function showTime() {

  // Create a Date object
  let time = new Date();

  // Get hours from time
  let hours = time.getHours();

  // Get minutes from time
  let minutes = time.getMinutes();

  // Get seconds from time
  let seconds = time.getSeconds();

  // Add 0 before single digit numbers
  if (hours < 10) hours = "0" + hours;
  if (minutes < 10) minutes = "0" + minutes;
  if (seconds < 10) seconds = "0" + seconds;

  // Display time in HH:MM:SS format
  document.getElementById("clock").innerText =
    hours + ":" + minutes + ":" + seconds;
}

// Call function every 1 second
setInterval(showTime, 1000);

Leave a Reply

Your email address will not be published. Required fields are marked *