Anyone please help me. | Sololearn: Learn to code for FREE!
¡Nuevo curso! ¡Todo programador debería aprender IA Generativa!
Prueba una lección gratuita
+ 2

Anyone please help me.

I have created a clock using js date function. How do I add zeros to te seconds and minutes. For example when it shows the time 3:30:7 , I want it to show like 03:30:07. Please help me. Here is the code https://sololearn.com/compiler-playground/WEzoRsvG7B92/?ref=app

4th Mar 2024, 11:55 AM
Web Developer AD
3 Respuestas
+ 5
Rather than manually constructing the string from the time components, you could also use the built-in formatter. var d = new Date(); heading1.textContent = d.toLocaleTimeString('en-GB'); The default time format for en-GB locale is 24 hours, with leading zeroes.
4th Mar 2024, 1:06 PM
Tibor Santa
Tibor Santa - avatar
+ 3
heading1.textContent = String(h).padStart(2, '0') + ":" + m + ":" + s;
4th Mar 2024, 12:12 PM
Bob_Li
Bob_Li - avatar
+ 1
Thanks Bob_Li & Tibor Santa it worked
5th Mar 2024, 12:54 AM
Web Developer AD