+ 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
3 Respuestas
+ 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);
+ 3
thanks man, I just had the wrong comparison operator. gracias
+ 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);