Live Digital Clock built with JavaScript


Live Digital Clock

This simple digital clock shows you the current time (GMT +1) and updates every second.

In .html file:

<label id="clock"></label>

In .js file:

const clock = document.getElementById('clock');

function refreshTime() {
let date = new Date();
clock.innerHTML = formatTime(date);

function formatTime(date) {
let hours = date.getHours();
let minutes = date.getMinutes();
let seconds = date.getSeconds();

hours = addZero(hours);
minutes = addZero(minutes);
seconds = addZero(seconds);

return `${hours}:${minutes}:${seconds}`
}


function addZero(time) {
time = time.toString();
return time.length < 2 ? "0" +time : time;
}
}

refreshTime()

setInterval(refreshTime, 1000)

Illustration for the Live Digital Clock built with JavaScript
Time illustrations by Storyset

Comments (0)


Be the first to leave a comment