Changing the date on a countdown when distance is < 0

36 Views Asked by At

I have a timer to countdown to a date and time. What I'd like it to do is that after it hits 0 with the first date, it would then start counting down to the next date.

I am not at all good at code so there might be the most simple answer to this but i have not been able to find it.

I tried making an if statement but I couldn't find what actually would tell it to do what I want it to.

    function countDown() {
    var countDownDate = new Date("feb 10, 2024 23:24:00").getTime();
  
    var x = setInterval(function() {
  
    var now = new Date().getTime();
    var distance = countDownDate - now;
  
    var h = Math.floor(distance / (1000 * 60 * 60 ));
    var m = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
    var s = Math.floor((distance % (1000 * 60)) / 1000);
    h = addZero(h);
    m = addZero(m);
    s = addZero(s);
    document.getElementById("timer").innerHTML = h + ":" + m + ":" + s ;


    }, 1000);
 
    function addZero(i) {
    if (i < 10) {i = "0" + i};  
    return i;
    }
 }

1

There are 1 best solutions below

2
phatfingers On

If your goal is to add one calendar day to countDownDate, preserving the same time of day, then you could do this:

if (distance < 0) {
    countDownDate.setDate(countDownDate.getDate() + 1);
    distance = countDownDate - now;
}