+ 1

Can someone give a little advice on this. For Ss and Gs I was trying to add a leading zero to the mins and secs if the length=1

https://code.sololearn.com/WqUOsu6iw3im/?ref=app

10th Jul 2017, 10:33 PM
James Barrow
James Barrow - avatar
3 ответов
+ 8
The standard time for min and sec is a range of numbers from 0 to 59. ... and you want to add a "0" to the front of the second or minute if they are 0..9, there's one thing to notice here 0-9 are all less than 10 so instead of saying [if (secs.lenght=1){}] you could say [if (secs < 10){}] here's the simplified code... //trying to make the min and sec display a leading 0 function printTime() { var d = new Date(); var hours = d.getHours(); var mins = d.getMinutes(); var secs = d.getSeconds(); if(secs < 10){ secs ="0"+secs; } if(mins < 10) { mins ="0"+mins; } document.body.innerHTML = hours+":"+mins+":"+secs; } setInterval(printTime, 1000);
10th Jul 2017, 10:48 PM
Nomeh Uchenna Gabriel
Nomeh Uchenna Gabriel - avatar
+ 3
thanks man, I just had the wrong comparison operator. gracias
10th Jul 2017, 10:50 PM
James Barrow
James Barrow - avatar
+ 1
Another way to add leading-zero to a 2digit string numbers. secs =("0"+secs).slice(-2); mins =("0"+mins).slice(-2); hours =("0"+hours).slice(-2);
7th Jun 2022, 1:13 PM
Calviղ
Calviղ - avatar