Skip to content

Instantly share code, notes, and snippets.

@zikes
Created Jan 21, 2013
Embed
What would you like to do?
Countdown
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Countdown</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<script>
var Time = {};
Time.second = 1000;
Time.minute = Time.second * 60;
Time.hour = Time.minute * 60;
Time.day = Time.hour * 24;
function get_time(){
var target_date = new Date("February 8, 2013 10:45 pm CST");
var now = new Date();
var diff = target_date.getTime() - now.getTime();
var days = 0;
var hours = 0;
var minutes = 0;
var seconds = 0;
var milliseconds = 0;
var output = [];
while(diff >= Time.day){
days++;
diff-=Time.day;
}
while(diff >= Time.hour){
hours++;
diff-=Time.hour;
}
while(diff >= Time.minute){
minutes++;
diff-=Time.minute;
}
while(diff >= Time.second){
seconds++;
diff-=Time.second;
}
milliseconds = diff;
if(days > 0){
output.push(days+" day"+(days > 1 ? "s" : ""));
}
if(hours > 0){
output.push(hours+" hour"+(hours > 1 ? "s" : ""));
}
if(minutes > 0){
output.push(minutes+" minute"+(minutes > 1 ? "s" : ""));
}
if(seconds > 0){
output.push(seconds+" second"+(seconds > 1 ? "s" : ""));
}
return output.join(', ');
}
$(function(){
var update = $('#display, title');
setInterval(function(){
update.text(get_time());
},1000);
});
</script>
</head>
<body>
<div id="display"></div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment