Skip to content

Instantly share code, notes, and snippets.

@BobHarper1
Created March 2, 2017 08:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BobHarper1/ca62ca2ac8048f4c982f34d50649ed41 to your computer and use it in GitHub Desktop.
Save BobHarper1/ca62ca2ac8048f4c982f34d50649ed41 to your computer and use it in GitHub Desktop.
Styled JavaScript Countdown Clock
<h1>Poll Countdown</h1>
<h2>#AE17 Northern Ireland Assembly Elections </h2>
<div id="clockdiv">
<div>
<span class="hours"></span>
<div class="smalltext">Hours</div>
</div>
<div>
<span class="minutes"></span>
<div class="smalltext">Minutes</div>
</div>
<div>
<span class="seconds"></span>
<div class="smalltext">Seconds</div>
</div>
</div>
<h3><a href="http://electionsni.org">ElectionsNI.org </a></h3>
<p>Polling stations close at 10pm on 2 March<br/>Find your <a href="http://www.eoni.org.uk/Home">polling station</a></p>
function getTimeRemaining(endtime) {
var t = Date.parse(endtime) - Date.parse(new Date());
var seconds = Math.floor((t / 1000) % 60);
var minutes = Math.floor((t / 1000 / 60) % 60);
var hours = Math.floor((t / (1000 * 60 * 60)) % 24);
var days = Math.floor(t / (1000 * 60 * 60 * 24));
return {
'total': t,
'days': days,
'hours': hours,
'minutes': minutes,
'seconds': seconds
};
}
function initializeClock(id, endtime) {
var clock = document.getElementById(id);
var daysSpan = clock.querySelector('.days');
var hoursSpan = clock.querySelector('.hours');
var minutesSpan = clock.querySelector('.minutes');
var secondsSpan = clock.querySelector('.seconds');
function updateClock() {
var t = getTimeRemaining(endtime);
hoursSpan.innerHTML = ('0' + t.hours).slice(-2);
minutesSpan.innerHTML = ('0' + t.minutes).slice(-2);
secondsSpan.innerHTML = ('0' + t.seconds).slice(-2);
if (t.total <= 0) {
clearInterval(timeinterval);
}
}
updateClock();
var timeinterval = setInterval(updateClock, 1000);
}
var deadline = new Date(Date.parse('March 2 2017 22:00:00 GMT+0000'));
initializeClock('clockdiv', deadline);
body{
text-align: center;
background: #00ECB9;
font-family: sans-serif;
font-weight: 100;
}
a:link{
 color: white;
}
a, a:link a:visited, a:active {
 color: #ffffff;
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
h1{
color: #26734b;
font-weight: 100;
font-size: 40px;
margin: 40px 0px 20px;
}
p, h2{
color: #26734b;
}
#clockdiv{
font-family: sans-serif;
color: #fff;
display: inline-block;
font-weight: 100;
text-align: center;
font-size: 30px;
}
#clockdiv > div{
padding: 10px;
border-radius: 3px;
background: #00BF96;
display: inline-block;
}
#clockdiv div > span{
padding: 15px;
border-radius: 3px;
background: #00816A;
display: inline-block;
}
.smalltext{
padding-top: 5px;
font-size: 16px;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment