0

im trying to countdown from the 2 dates that i have and i have the following code :

jquery

var end_date= $('#end_date').val()
var start_date= $('#start_date').val()

var countDownDate = end_date* 1000;
var now = start_date* 1000;


var x = setInterval(function() {


   now = now + 1000;
    

  var distance = countDownDate - now;
    

  var months = Math.floor( distance / (1000 * 60 * 60 * 24 * 30 ) );
  var days = Math.floor(distance / (1000 * 60 * 60 * 24));

  $("#demo").html(months+ " M " + days + " D " );
    
  if (distance < 0) {
    clearInterval(x);
    $("demo").css("background-color", "red")
  }
}, 1000);

HTML

    <p id="demo"></p>

<input type="hidden" id="start_date" value="1509728400" />
<input type="hidden" id="end_date" value="1699030800" />

the result that i get with the following code above is 73 Months 2190 Days left and i want something like this 73 Months 29 Days left. thanks in advance

PinballWizard
  • 131
  • 1
  • 2
  • 12
  • Please add the relevant HTML code too so we can see the date format your inputs are using. The output I get is NaN: https://jsfiddle.net/RoryMcCrossan/cog903pa/ – Rory McCrossan Jun 29 '22 at 07:27
  • 1
    okay i edited it again – PinballWizard Jun 29 '22 at 07:44
  • The issue is because you're calculating the *total* number of days, not the number of days remaining after calculating the months. See the duplicate for the correct logic you need to use in this situation. – Rory McCrossan Jun 29 '22 at 07:50

0 Answers0