0

hello everyone i'm working on a clock timer i wrote the code in angular code it work very fine

let timer: string = "0:00:00"; startPause() { this.play = false; this.pause = true; let sec = 0; let min = 0; let hour = 0; setInterval(() => { if(sec === 59) { min++; sec = 0; } else if(min > 59) { hour++; min = 0; } else { sec++; } this.timer = `${hour}:${min}:${sec}`; }, 10); } 

the output is correct but when i want to add zero before seconds or minutes or hours if they are < 10 i write this sec = "0" + sec it tells me the String Type Is Not assignable to sec thank u.

1
  • 1
    Create a separate padding function (as described in this answer), then just use it within the string template (like ${pad(hour)}:${pad(min)}... etc.) Commented Aug 2, 2017 at 12:19

1 Answer 1

0

Try something like this:

... let strSec = String(sec); if (sec < 10) { strSec = '0' + strSec; } ... //same for hour and min this.timer = `${strHour}:${strSec}:${strSec}`; 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.