amitabhaghosh197
1/1/2015 - 12:36 PM

DAte Counter javascript

DAte Counter javascript

<script>
  function DayCounter(initDate, id){

    this.counterDate = new Date(initDate);

    this.container = document.getElementById(id);

    this.calculate();

}

  

DayCounter.prototype.calculate=function(){

    var secDiff = Math.round(((new Date()) - this.counterDate)/1000);

    var nextUpdate = (nextUpdate = (secDiff % 86400)) < 0? (nextUpdate*-1) : (86400-nextUpdate);

    var tmp = Math.abs((tmp = secDiff/86400)) < 1? 0 : tmp;

    var days = (tmp < 0 ? Math.ceil(tmp) : Math.floor(tmp));

    this.container.innerHTML =

        "<strong>" + days + "</strong> " + (Math.abs(days) == 1? "day" : "days");

    var self = this;

    setTimeout(function(){self.calculate();}, (++nextUpdate*1000));

}

 

window.onload=function(){ new DayCounter('September 12, 2009 00:00:00', 'counter'); }
  
</script>

<div id="counter"></div>