I asked about a day ago how to make a clock. The code I'm using works, but instead of going from 18:59:59
to 19:00:00
, it goes from 18:59:59
to 19:0:0
. How would I add a zero upfront if something is a single digit, so instead of 19:0:0
, it becomes 19:00:00
? Here is the code I'm using.
setInterval(function(){
let i = 5;
const timer = setInterval(() => {
var date = new Date();
var time = date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds()
document.getElementById('clock').innerHTML = time;
i--;
if (i < 0) {
clearInterval(timer)
}
}, 1000);
});
<p id="clock"></p>
Thanks!