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.

aleclarson
  • 18,087
  • 14
  • 64
  • 91
  • 1
    Create a separate padding function (as described in [this answer](https://stackoverflow.com/a/40717803/1229023)), then just use it within the string template (like `${pad(hour)}:${pad(min)}...` etc.) – raina77ow Aug 02 '17 at 12:19

1 Answers1

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}`;