New course! Every coder should learn Generative AI!
Try a free lesson+ 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
3 Answers
+ 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.
+ 3
heading1.textContent =
String(h).padStart(2, '0') + ":" +
m + ":" + s;
+ 1
Thanks Bob_Li & Tibor Santa it worked