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

101
Visualizações
Countdown based on timezone with jQuery

I worked with this:

(function() {

  var timeElement, eventTime, currentTime, duration, interval, intervalId;

  interval = 1000; // 1 second

  // get time element
  timeElement = document.querySelector("#countdown");
  // calculate difference between two times
  eventTime = moment.tz("2022-05-29T08:00:00", "Europe/Berlin");
  // based on time set in user's computer time / OS
  currentTime = moment.tz();
  // get duration between two times
  duration = moment.duration(eventTime.diff(currentTime));

  // loop to countdown every 1 second
  setInterval(function() {
    // get updated duration
    duration = moment.duration(duration - interval, 'milliseconds');

    // if duration is >= 0
    if (duration.asSeconds() <= 0) {
      clearInterval(intervalId);
      // hide the countdown element
      timeElement.classList.add("hidden");
    } else {
      // otherwise, show the updated countdown
      timeElement.innerText = duration.days() + ":" + duration.hours() + ":" + duration.minutes() + ":" + duration.seconds();
    }
  }, interval);

}());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.1/moment-with-locales.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.17/moment-timezone-with-data-2012-2022.min.js"></script>

<div id="countdown"></div>

Unfortunately, the time doesn't get calculated correctly. If you change for example 2022-05-29 to 2022-08-29, nothing changes. Additionally, every value should have two digits.

Has someone an idea to fix that? Would be very thankful!

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Your question about momentjs is answered here; use asDays() not days()

You can pad a string with 0's using the function padStart(length, withWhat) Mozilla Developer Network Documentation

//...snip...
    } else {
      // otherwise, show the updated countdown
      timeElement.innerText =
        duration.asDays().toFixed().padStart(2, 0) + ":" +
        duration.hours().toString().padStart(2, 0) + ":" +
        duration.minutes().toString().padStart(2, 0) + ":" + 
        duration.seconds().toString().padStart(2, 0);
    }
//...snip...
about 4 years ago · Juan Pablo Isaza Relatório
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