Skip to content

Instantly share code, notes, and snippets.

@monkianer
Created August 3, 2015 08:31
Show Gist options
  • Save monkianer/a69952aaa4590cf978d6 to your computer and use it in GitHub Desktop.
Save monkianer/a69952aaa4590cf978d6 to your computer and use it in GitHub Desktop.
simple count down timer.
var target_date = new Date("Aug 6, 2015").getTime();
// variables for time units
var days, hours, minutes, seconds;
function updateCountdown(){
// get current time stampe
var current_date = new Date().getTime();
// get seconds by using target_date - current_date, divide by million second.
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 = parseInt(seconds_left / 60);
seconds = parseInt(seconds_left % 60);
var daysString = days.toString();
if(daysString.length < 2){
daysString = '0' + daysString;
}
var hoursString = hours.toString();
if(hoursString.length < 2){
hoursString = '0' + hoursString;
}
var minutesString = minutes.toString();
if(minutesString.length < 2){
minutesString = '0' + minutesString;
}
var secondsString = seconds.toString();
if(secondsString.length < 2){
secondsString = '0' + secondsString;
}
$('.counter .day').text( daysString );
$('.counter .hour').text( hoursString );
$('.counter .minute').text( minutesString );
$('.counter .second').text( secondsString );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment