0
Can you help me with a clock?
I want to put a 0 before the minutes and seconds when itās just one number. I want for example: 16:09:06 And not: 16:9:6 https://code.sololearn.com/Wq4QwbnrGnRE/?ref=app
3 Answers
+ 6
Just create a condition that if minutes and seconds get less than 10 then add a "0" in front.
In your code, you can do following:
if(seconds<10){
//document.write("0"+seconds);
seconds= "0"+seconds;
}
if(minutes<10){
//document.write("0"+);
minutes ="0"+minutes;
}
additionally if you want to show time in 12-hr clock with "am" and "pm" , you can following:
var ampm="am";
if(hours>12){
hours= hours -12;
ampm= "pm"
}
document.body.innerHTML=hours+":"+minutes+":"+seconds+" "+ampm;
}
+ 3
Just change to
var minutes= ("0"+t.getMinutes()).slice(-2);
var seconds= ('0'+t.getSeconds()).slice(-2);