JS DOM: Digital Clock

By Xah Lee. Date: . Last updated: .

Here is JavaScript example that prints the current time.

code

<div id="output_s5sgg"></div>
{
 // 2026-06-18

 const output_s5sgg = document.getElementById("output_s5sgg");

 // conver number num to string, pad 0 in front to make it 2 digits
 const padNum = (num) => num.toString().padStart(2, "0");

 const updateDisplay = () => {
  const newdate = new Date();
  const hh = newdate.getHours();
  const mm = newdate.getMinutes();
  const ss = newdate.getSeconds();
  output_s5sgg.textContent = `${padNum(hh)}:${padNum(mm)}:${padNum(ss)}`;
 };

 updateDisplay();
 setInterval(updateDisplay, 1000);
}