Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

132
Visualizações
Single digit if-statements not working for time (4:22:22 showing instead of 04:22:22)?

I have this simple program that takes a time from the future and creates a countdown until that time:

var x = setInterval(function() {

            // Get today's date and time
            var now = new Date().getTime();
  
            // Find the distance between now and the count down date
            var distance = Number(String(timerOrigin.innerHTML)+"000") - now;
  
            // Time calculations for days, hours, minutes and seconds

            //Days are not displayed, only used to help calculate hours
            var days = Math.floor(distance / (1000 * 60 * 60 * 24));


            var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)) + (days * 24);
            if (hours.length == 1) {
                hours= "0" + hours;
            }


            var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
            if (minutes.length == 1) {
                minutes= "0" + minutes;
            }


            var seconds = Math.floor((distance % (1000 * 60)) / 1000);

            if (seconds.length == 1) {
                seconds = "0" + seconds;
            }
                
            // Display the time
            timer.innerHTML = hours + ":" + minutes + ":" + seconds;
  
            // If the count down is finished, Display score
            if (distance < 0) {
            clearInterval(x);
            timer.innerHTML = homeTeamScore.innerHTML + "-" + awayTeamScore.innerHTML;
            }
        }, 1000);

//Push to Dom
            sideScore.appendChild(timer)

The issue I'm having is that the single-digit if statements are not working.

var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)) + (days * 24);
            if (hours.length == 1) {
                hours= "0" + hours;
            }

this will return 4:22:22 whereas the desired result is 04:22:22. I know I'm missing something somewhere but I cannot put my finger on it. I also ran some log tests and the program is not even getting into the if statements. The console returns no errors as well. Perhaps it's an issue with the scoping since this is all inside of a loop? Any help is appreciated.

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda