JSFiddle - React, Tailwind, and code Playground

by ian_smithz

HTML

<div id="countdown"></div>

JavaScript

function secsToDHMS(s)
{
  var x = [86400, 3600, 60, 1],z,i=-1;
  while (z=x[++i])
        x[i] = ("0" + parseInt(s / z,10)).slice(-2),
        s %= z;

    return x.join(':');
};


(function() {
    
    var currentDate      = new Date();
    var endDate          = new Date('March 30, 2012 18:10:00');
    var secsToGo         = (new Date(endDate - currentDate)).getTime() / 1000;
    var countdownElement = document.getElementById('countdown');
    
    var timer = setInterval(function() {
        
        if (--secsToGo < 0) {
            clearInterval(timer);
            return;
        }
       
        countdownElement.innerHTML = secsToDHMS(secsToGo);
        
    }, 1000);
    
})();