0

JavaScript Date() function

Hello, SoloLearners. I've been playing with js a little bit and tried to make a simple clock function that would display time in html document. It's pretty easy to do it, and I had no problems at all, but one thing that's bugging me is seconds. For example, variable s represents seconds. If s is less than 9, let's say it's 5 seconds in the current minute, seconds are displayed as "5". Is there a way to make it display as "05"? I'm pretty sure that the answer is easy, but I couldn't figure it out. Thanks.

1st Nov 2016, 9:35 PM
Petar Mijailović
Petar Mijailović - avatar
2 Answers
+ 2
I don't know what your code is but maybe this will help you figure it out: if(s <= 9) second_string = "0" + s; else second_string = "" + s; It will put a zero at the beginning of second_string if s has only one digit
1st Nov 2016, 11:11 PM
Bekir Uzun
Bekir Uzun - avatar
0
use do-while loops to add one by one units to your counters using setInterval function to call it every 1000 ms. Thats a way to see it...
3rd Jul 2020, 3:13 AM
Dany Renaudier
Dany Renaudier - avatar