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

251
Visualizações
How can i reduce the "Days" to 30 in this Countdown Timer pop-up?

I have made a countdown timer pop-up with the help of google using javascript code which is set to end after 100 days, I am not able to change it to 30 days after many tries. As I am new to javascript, please help and solve my problem, also I want to know the proper logic behind this.

This is what I tried (This results as 100 days and so on hour,min, sec)

window.addEventListener("load", function(){ setTimeout( function open(event){ document.querySelector(".popup").style.display = "block"; }, 2000 ) });

document.querySelector("#close").addEventListener("click", function(){ document.querySelector(".popup").style.display = "none"; });

const year = new Date().getFullYear(); const fourthOfJuly = new Date(year, 6, 4).getTime(); const fourthOfJulyNextYear = new Date(year + 1, 6, 4).getTime(); const month = new Date().getMonth(); // countdown let timer = setInterval(function () { // get today's date const today = new Date().getTime(); // get the difference let diff; if (month > 6) { diff = fourthOfJulyNextYear - today; } else { diff = fourthOfJuly - today; } // math let days = Math.floor(diff / (1000 * 60 * 60 * 24)); let hours = Math.floor((diff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)); let minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60)); let seconds = Math.floor((diff % (1000 * 60)) / 1000); // display document.getElementById("timer").innerHTML = ' \ ' + days + 'days \ \ ' + hours + 'hours \ \ ' + minutes + 'minutes \ \ ' + seconds + "seconds \ "; }, 1000);

I want it to show 30 days, but how?

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

0

your code shows a countdown to the next 4th of July.

Do you want to display a countdown to a specific date or just always 30 days?

In your interval (that runs approximately each second) you compare the target date with the current date and then calculate your days, hours, minutes.

const today = new Date()

// targetDate = today + 30 days
const targetDate = new Date();
targetDate.setDate(targetDate.getDate() + 30);

const diff = targetDate.getTime() - today.getTime()

Here you can find a working version: https://jsfiddle.net/6bhuywn4/26/

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