How to Make a Digital Clock With HTML, CSS and JavaScript

Updated · 5 min read · By the Hacker Typer team

A clock is a classic first JavaScript project. It teaches how to read the time, how to update the page, and how to repeat something every second. The full file is below and takes a minute to set up.

Digital clock showing 09:41:07 above the JavaScript that reads the time and updates every second
See a finished one: our Flip Clock uses the same idea with three themes.

The complete code

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Clock</title>
  <style>
    body {
      margin: 0; height: 100vh; background: #000;
      display: flex; align-items: center; justify-content: center;
    }
    #clock {
      font: bold 18vw "Courier New", monospace;
      color: #00ff66;
    }
  </style>
</head>
<body>
  <div id="clock">00:00:00</div>

  <script>
    const clock = document.getElementById("clock");

    function twoDigits(number) {
      return String(number).padStart(2, "0");   // 7 becomes "07"
    }

    function showTime() {
      const now = new Date();
      const hours = twoDigits(now.getHours());
      const minutes = twoDigits(now.getMinutes());
      const seconds = twoDigits(now.getSeconds());
      clock.textContent = hours + ":" + minutes + ":" + seconds;
    }

    showTime();                   // show the time straight away
    setInterval(showTime, 1000);  // then update every second
  </script>
</body>
</html>

How to use this code

  1. Get the file. Press Download above the code to save it as clock.html. Or press Copy code, paste it into a plain text editor (Notepad on Windows, TextEdit in plain-text mode on a Mac, or a code editor such as VS Code) and save it as clock.html. The name must end in .html, not .txt.
  2. Open it. Double-click the file. It opens in your web browser and starts working straight away.
  3. Change it. Open the same file in the text editor, edit something, save, and refresh the browser page to see the result.

Nothing needs to be installed, and the file works without an internet connection.

How the code works

Easy changes to try

12-hour time with AM and PM

Replace the hours line and the last line of showTime with:

const hour24 = now.getHours();
const hours = twoDigits(hour24 % 12 || 12);
const ampm = hour24 < 12 ? " AM" : " PM";
clock.textContent = hours + ":" + minutes + ":" + seconds + ampm;

hour24 % 12 gives the remainder after dividing by 12, so 15 becomes 3. The || 12 turns a result of 0 into 12, for midnight and noon.

Hide the seconds

Change the last line of showTime to clock.textContent = hours + ":" + minutes;

Show the date

Add <div id="date"></div> under the clock, give it a smaller font size, and inside showTime add document.getElementById("date").textContent = now.toDateString();

Change the look

Edit color and background for other colours, and swap the font name for any font on your computer.

FAQ

Where does the clock get the time from?

From the device it runs on. It is as accurate as the computer's or phone's own clock and uses its time zone.

Why is the clock a second behind sometimes?

The timer is not perfectly exact, so an update can land slightly after the second changes. Updating four times a second, with 250 instead of 1000, hides this.

Does it work offline?

Yes. The file needs no internet connection.

More tools

See all tools

Keep reading