How to create Digital Clock in java script

Hello friends today i am sharing with you digital clock in java script how to write a code in java script and where clock can be digital clock

The clock is basicaly of two types, analog and digital in this section hour minues seconds and mili seconds in div tag

<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>replit</title>
  <link href="style.css" rel="stylesheet" type="text/css" />
</head>

<body>
  <div id="digiClock"></div>
  <script src="script.js"></script>


  <script src="https://replit.com/public/js/replit-badge.js" theme="blue" defer></script>
</body>

</html>
setInterval(nowTime, 1000);
function nowTime() {
  let time = new Date();
  let hour = time.getHours();
  let miliSeconds = time.getMilliseconds();
  let seconds = time.getSeconds();
  let minut = time.getMinutes();
  timeZone = "PM";

  if (hour > 12) {
    hour -= 12;
    timeZone = "PM";
  } if (hour == 0) {
    hr = 12;
    timeZone = "AM";
  }

  hour = hour < 10 ? "0" + hour : hour;
  minut = minut < 10 ? "0" + minut : minut;
  seconds = seconds < 10 ? "0" + seconds : seconds;

  let currentTime = hour + ":" + minut + ":" + seconds + " :" + miliSeconds + ":" + timeZone;

  document.getElementById('digiClock').innerHTML = currentTime;

}

Leave a Comment