var endDate; var serverDate = new Date('February 23, 2012 07:26:55'); function timeStampToDate(timestamp) { var regex=/^([0-9]{2,4})-([0-1][0-9])-([0-3][0-9]) (?:([0-2][0-9]):([0-5][0-9]):([0-5][0-9]))?$/; var parts=timestamp.replace(regex,"$1 $2 $3 $4 $5 $6").split(' '); return new Date(parts[0],parts[1]-1,parts[2],parts[3],parts[4],parts[5]); } function initCountdown(endTimestamp){ endDate = timeStampToDate(endTimestamp); updateCountdown(); } function updateCountdown(){ var diff = Math.floor((endDate - serverDate)/1000); if(diff<0){ document.getElementById("countdown-timer").innerHTML = "Too late!"; } else { hours = Math.floor(diff/3600); minutes = Math.floor((diff-hours*3600)/60); seconds = diff % 60; if(hours<10){ hours = "0" + hours; } if(minutes<10){ minutes = "0" + minutes; } if(seconds<10){ seconds = "0" + seconds; } document.getElementById("countdown-timer").innerHTML = hours + ":" + minutes + ":" + seconds; serverDate.setSeconds(serverDate.getSeconds()+1); setTimeout("updateCountdown()", 1000); } }