0

Can you help me with a js script?

function printTime() { var d = new Date(); var hours = d.getHours(); var mins = d.getMinutes(); var secs = d.getSeconds(); document.body.innerHTML = hours+":"+mins+":"+secs; } setInterval(printTime, 1); When the seconds are <10 the browser writes 1,2 ,3 ,6, 9, however I want it to write 01, 05, 09 and so on. It's the same problem with the minutes. I tried it with the if statement. Can someone explain the code of rhis to me pls.

10th Nov 2019, 6:40 PM
Marci MihĂĄlka
Marci MihĂĄlka - avatar
2 Answers
+ 4
if (secs<9) secs="0" + secs The same goes for minutes. Also, the interval will work just fine if you set it at 1000.
10th Nov 2019, 7:02 PM
🍇 Alex Tușinean 💜
🍇 Alex Tușinean 💜 - avatar
0
I wrote it w document.body.innerHTML and it didnt work
10th Nov 2019, 7:04 PM
Marci MihĂĄlka
Marci MihĂĄlka - avatar