I found a very simple and functional script to do countdown, but it has a problem; when the counter reaches 9, it no longer has two characters, something that ends up spoiling quite the alignment I had to do to structure the CSS. Does anyone know how to solve by inserting a "0" in front of numbers smaller than 9?
<div class="contador_ajuste_largura">
<div class="contador" >
<script type="text/javascript">
function atualizaContador(YY,MM,DD,HH,MI,saida) {
var SS = 00;
var hoje = new Date();
var futuro = new Date(YY,MM-1,DD,HH,MI,SS);
var ss = parseInt((futuro - hoje) / 1000);
var mm = parseInt(ss / 60);
var hh = parseInt(mm / 60);
var dd = parseInt(hh / 24);
ss = ss - (mm * 60);
mm = mm - (hh * 60);
hh = hh - (dd * 24);
var faltam = '';
faltam += (dd && dd > 1) ? dd+' : ' : (dd==1 ? '1 dia, ' : '');
faltam += (toString(hh).length) ? hh+' : ' : '';
faltam += (toString(mm).length) ? mm+' : ' : '';
faltam += (toString(mm).length) ? ss+' ' : '';
if (dd+hh+mm+ss > 0) {
document.getElementById(saida).innerHTML = faltam;
setTimeout(function(){atualizaContador(YY,MM,DD,HH,MI,saida)},1000);
} else {
document.getElementById(saida).innerHTML = '';
setTimeout(function(){atualizaContador(YY,MM,DD,HH,MI,saida)},1000);
}
}
window.onload=function(){
atualizaContador('2017','11','23','23','59','elemento');
}
</script>
</div>
</div>