cibgraphics
4/2/2014 - 9:47 PM

Countdown javascript

Countdown javascript

// set the date we're counting down to
  var target_date = new Date("August 12, 2014 17:35").getTime();
   
  // variables for time units
  var days, hours, minutes, seconds;
   
  // get tag element
  var countdown = document.getElementById("countdown");

  function pad2(number) {
     return (number < 10 ? '0' : '') + number
  } 
   
  // update the tag with id "countdown" every 1 second
  setInterval(function () {
   
    // find the amount of "seconds" between now and target
    var current_date = new Date().getTime();
    var seconds_left = (target_date - current_date) / 1000;
 
    // do some time calculations
    days = parseInt(seconds_left / 86400);
    seconds_left = seconds_left % 86400;
     
    hours = parseInt(seconds_left / 3600);
    seconds_left = seconds_left % 3600;
     
    minutes = pad2(parseInt(seconds_left / 60));
    seconds = pad2(parseInt(seconds_left % 60));

    var daysName = (days > 1) ? ' days - ' : ' day - ';
     
    // format countdown string + set tag value
    /*countdown.innerHTML = days + " days, " + hours + " hours, "
    + minutes + " minutes, " + seconds + " seconds"; */
    if ($('#countdown').length > 0) { 
      countdown.innerHTML = minutes + ':' + seconds; 
    }
    
   
  }, 1000);