Home > OS >  Check if the time distance is between different times
Check if the time distance is between different times

Time:12-09

I made a countdown using javascript and php from my functions the countdown works but now I want to have 3 options:

  1. if the countdown is longer than 24 hours show te selector.next(".countdown").html(expiry); date

  2. if the countdown is 6 hours or less show the timer selector.next(".countdown").html(days "d " hours "h " minutes "m " seconds "s ");

  3. else the countdown is less than 0 show that the endend selector.next(".countdown").html(<p>Closed</p>);

$(".expiry").each(function() {
    var expiry = new Date($(this).text());
    var selector = $(this)
    var x = setInterval(function() {
        var currentDateObj = new Date();
        var numberOfMlSeconds = currentDateObj.getTime();
        var addMlSeconds = 60 * 60 * 1000;
        var now = new Date(numberOfMlSeconds - addMlSeconds);

        var distance = expiry - now;
        var days = Math.floor(distance / (1000 * 60 * 60 * 24));
        var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
        var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
        var seconds = Math.floor((distance % (1000 * 60)) / 1000);

        if( distance >= 86400000 && distance < 21600000){
          selector.next(".countdown").html(expiry);  
        }else if ( distance <= 21600000 && distance > 0){
          selector.next(".countdown").html(days   "d "   hours   "h "   minutes   "m "   seconds   "s "); 
        }else{
          selector.next(".countdown").html('<p>error</p>');
        }
    }, 1000);
});

CodePudding user response:

Is the first line of your IF statement correct? Are you wanting to check if it's between those 2 numbers? If so, should the condition not be:

if(distance <= 86400000 && distance > 21600000)

I.E. If the distance is LESS THAN OR EQUAL TO 86400000 AND GREATER THAN 21600000

At the moment you're checking if distance is both GREATER THAN OR EQUAL TO 86400000 and LESS THAN 21600000 which will always produce false

EDIT See full if statement block with condition for GREATER THAN 86400000

if (distance > 86400000) {
    selector.next(".countdown").html('<p>A LOOOOOONG Way off expiring</p>');            
}
else if (distance <= 864000000 && distance > 432000000) { 
    selector.next(".countdown").html(expiry); 
}
else if (distance <= 432000000 && distance > 0) {
    selector.next(".countdown").html(days   "d "   hours   "h "   minutes   "m "   seconds   "s "); 
}
else if (distance < 0) { 
    selector.next(".countdown").html('<p>afgelopen</p>'); 
}
else { 
    selector.next(".countdown").html('<p>Error</p>'); 
}
  • Related