JavaScript: 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

SVG Clock

βˆ‘ JS in Depth
XAH Β BUY NOW

JS in Depth

JS Obj Ref

DOM


DOM

Overview

Basic Examples

HTML Input

Web Scripting Examples

Web Scripting Misc

jQuery

node.js