JavaScript: Digital Clock

By Xah Lee. Date: . Last updated: .

Here is JavaScript example that prints the current time.

Here is the code:

<p><span id="display27610"></span></p>
{
const display27610 = document.getElementById("display27610");

// 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 dd = new Date();
    const hh = dd.getHours();
    const mm = dd.getMinutes();
    const ss = dd.getSeconds();
    display27610.textContent = `${padNum(hh)}:${padNum(mm)}:${padNum(ss)}`;
});

setInterval(updateDisplay, 1000);
}

Analog Clock

SVG Clock

BUY Ξ£JS JavaScript in Depth