TheSmile
TheSmile

Reputation: 360

Jquery countdown for day

I have a countdown jquery working for hour, minutes and second. So far, this worked perfectly. However I want it also count on day

This is the JS FIDDLE

HTML

<div id="counttime">24:00:00</div>
<div id="cons"></div>

JQUERY

    function pad(num) {
  return num<10?"0"+num:num;
}
$(function() {
  var tCont = $('#counttime');
  var timer = tCont.html().split(':');

  if (timer.length === 2) timer.unshift(0);
  var endMilli= timer[0]*60*60*1000;
  endMilli += timer[1]*60*1000
  endMilli += timer[2]*1000;
  if(endMilli > 0) {
    var endTime = new Date(Date.now()+endMilli);   
$("#cons").append("<br>"+endTime);
    var tId = setInterval(function() {
      var diff = endTime.getTime()-Date.now();
      if (diff<=0) {
        tCont.html("00:00:00");
        clearInterval(tId);
      }
      else {
        var d = new Date(diff);
        hh = pad(d.getUTCHours()); 
        mm = pad(d.getMinutes());
        ss = pad(d.getSeconds());
        tCont.html(""+hh+":"+mm+":"+ss);
      }
    },300);      
  }
});

Upvotes: 0

Views: 404

Answers (2)

PeterKA
PeterKA

Reputation: 24648

You would have to adjust your code to allow for 4 parts and then add a line to evaluate the days:

dd = pad( (diff - diff % (864e5))/864e5 ),

DEMO

Upvotes: 1

rodrigogq
rodrigogq

Reputation: 1953

Your hh part should multiply by days!

hh = pad(d.getUTCHours() + d.getDay()*24); 

You should add months as well... not sure if this is easy. Or you can try this:

var hours = Math.abs(date1 - date2) / 36e5;
hh = pad(hours); 

Where 36e5 = 60*60*1000 milliseconds

Upvotes: 0

Related Questions