mardi 3 novembre 2020

Switch from days to hours after a certain time

Goal: Once the time hits two days or less, then it switches to hours instead of days.

Problem: In checking code by changing the date, it still only shows the days left, and not the hours. Am I missing something?

var countDownDate = new Date("Nov 15, 2020 11:59:59").getTime();

var x = setInterval(function() {

  var now = new Date().getTime();
    
  var distance = countDownDate - now;
    
  var days = Math.floor(distance / (1000 * 60 * 60 * 24));
  var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
    
    
 if (distance < 2) {
    clearInterval(x);
    document.getElementById("aep-countdown-date").innerHTML = hours + " hours left";
  } 
    
  else if (distance < 0) {
    clearInterval(x);
    document.getElementById("aep-countdown-date").innerHTML = "The time has ended.";
  }
    
  else  {
      clearInterval(x);
      document.getElementById("aep-countdown-date").innerHTML = days + " days left";
  }
});

Aucun commentaire:

Enregistrer un commentaire