Can you help me with a clock? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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

30th Aug 2018, 3:14 PM
Artur Spain 🇪🇸
2 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; }
30th Aug 2018, 3:43 PM
Шащи Ранжан
Шащи Ранжан - avatar
+ 3
Just change to var minutes= ("0"+t.getMinutes()).slice(-2); var seconds= ('0'+t.getSeconds()).slice(-2);
30th Aug 2018, 4:05 PM
Calviղ
Calviղ - avatar