Skip to content

Instantly share code, notes, and snippets.

@olucasmac
Forked from noahub/count_to_date.js
Created July 13, 2018 11:44
Show Gist options
  • Save olucasmac/dbfd6ff16fbd8ae1fa36e23a1c3b2a12 to your computer and use it in GitHub Desktop.
Save olucasmac/dbfd6ff16fbd8ae1fa36e23a1c3b2a12 to your computer and use it in GitHub Desktop.
Add a Countdown to a Date
<script>
countdown('06/26/2017 8:00 PM', 'timer'); //date format: mm/dd/yyyy hh:mm AM
function countdown(dt, id)
{
var end = new Date(dt);
var _second = 1000;
var _minute = _second * 60;
var _hour = _minute * 60;
var _day = _hour * 24;
var timer;
function showRemaining() {
var now = new Date();
var distance = end - now;
if (distance < 0) {
clearInterval(timer);
document.getElementById(id).innerHTML = 'THE DAY HAS ARRIVED!'; //Displays when countdown is complete
return;
}
var days = Math.floor(distance / _day);
var hours = Math.floor((distance % _day) / _hour);
var minutes = Math.floor((distance % _hour) / _minute);
var seconds = Math.floor((distance % _minute) / _second);
document.getElementById(id).innerHTML = days + ' days ';
document.getElementById(id).innerHTML += hours + ' hrs ';
document.getElementById(id).innerHTML += minutes + ' mins ';
document.getElementById(id).innerHTML += seconds + ' secs';
}
timer = setInterval(showRemaining, 1000);
}
/**
* Do not remove this section; it allows our team to troubleshoot and track feature adoption.
* TS:0002-03-083
*/
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment