Simple countdown timer
Trying to get to the simplest way to countdown to a specific date
by ian_smithz
HTML
<div id="countdown"></div>
JavaScript
//Global variables
var monthNames = [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ];
//Countdown function
function countdown (){
//Get today's date
today = new Date();
//Setup the final date in UTC time
BigDay = new Date(Date.UTC(2018,04,03,23,59));
//Calculate the time left from now till the final date
timeLeft = (BigDay.getTime() - today.getTime());
daysLeft = Math.floor(timeLeft / (24 * 60 * 60 * 1000));
hrsLeft = Math.floor(timeLeft / (1000*60*60)%24);
minsLeft = Math.floor(timeLeft / (1000*60)%60);
secsLeft= Math.floor(timeLeft / 1000)%60;
//What to display in the browser
document.getElementById('countdown').innerHTML = daysLeft + " days " + hrsLeft +" hours " + minsLeft + " minutes and "+secsLeft+" seconds left";
//Show the final date
document.getElementById('countdown').innerHTML += "<br>until "+monthNames[BigDay.getUTCMonth()] + " " + BigDay.getUTCDate() + " " + BigDay.getUTCFullYear() + " "+ BigDay.getUTCHours() + ":" + BigDay.getUTCMinutes();
//Show the final date in local time
document.getElementById('countdown').innerHTML += "<br>which is "+monthNames[BigDay.getMonth()] + " " + BigDay.getDate() + " " + BigDay.getFullYear() + " "+ BigDay.getHours() + ":" + BigDay.getMinutes()+" local time";
}
//Refresh the countdown function every second
setInterval(countdown, 1000);