Skip to content

Instantly share code, notes, and snippets.

@jalal
Created February 3, 2015 18:14
Show Gist options
  • Save jalal/7d46b44c4f7b60010f73 to your computer and use it in GitHub Desktop.
Save jalal/7d46b44c4f7b60010f73 to your computer and use it in GitHub Desktop.
JS Countdown timer
var target = document.getElementById('target');
var current = document.getElementById('current');
var d = new Date();
current.innerHTML = "Now: " + d;
var opening = new Date(d.getFullYear(), d.getMonth(), d.getDate(), '16', '00', '00');
var closing = new Date(d.getFullYear(), d.getMonth(), d.getDate(), '22', '00', '00');
target.innerHTML = "Open: " + opening;
var el = document.getElementById('result');
var cdown;
var result = '';
function showTime() {
d = new Date();
if( (d > opening) && (d < closing)) {
cdown = new Date(closing - d);
result = 'Lines will close in ' + ('0'+cdown.getHours()).slice(-2) + ':' + ('0'+cdown.getMinutes()).slice(-2) + ':' + ('0'+cdown.getSeconds()).slice(-2);
} else {
cdown = new Date(opening - d);
result = 'Lines will open in ' + ('0'+cdown.getHours()).slice(-2) + ':' + ('0'+cdown.getMinutes()).slice(-2) + ':' + ('0'+cdown.getSeconds()).slice(-2);
}
el.innerHTML = result;
}
window.setInterval(showTime, 1000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment