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?
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/