JS: Digital Clock

By Xah Lee. Date: . Last updated: .

Here's JavaScript example that prints the current time.

Here's 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

JS: SVG Clock

DOM How-To


Web Scripting Overview


HTML Input


Web Scripting Examples


Web Scripting Misc


jQuery


node.js

Like it? Help me by telling your friends. Or, Put $5 at patreon.

Or, Buy JavaScript in Depth

If you have a question, put $5 at patreon and message me.

Web Dev Tutorials