Last active
January 7, 2019 10:45
-
-
Save PerpetualBeta/3964527 to your computer and use it in GitHub Desktop.
A real-time countdown timer for a web page. Will countdown to a target time, but only on days specified (eg: Mon-Fri).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (document.getElementById('countdownTimer')) { | |
pad = function(n, len) { // leading 0's | |
var s = n.toString(); | |
return (new Array( (len - s.length + 1) ).join('0')) + s; | |
}; | |
var timerRunning = setInterval( | |
function countDown() { | |
var now = new Date(); | |
if ( (now.getDay() >= 1) && (now.getDay() <= 5) ) { // Monday to Friday only | |
var target = 15; // 15:00hrs is the cut-off point | |
if (now.getHours() < target) { // don't do anything if we're past the cut-off point | |
var hrs = (target - 1) - now.getHours(); | |
if (hrs < 0) hrs = 0; | |
var mins = 59 - now.getMinutes(); | |
if (mins < 0) mins = 0; | |
var secs = 59 - now.getSeconds(); | |
if (secs < 0) secs = 0; | |
var str = pad(hrs, 2) + ':' + pad(mins, 2) + '.<small>' + pad(secs, 2) + '</small>'; | |
document.getElementById('countdownTimer').innerHTML = str; | |
} | |
} | |
}, 1000 | |
); | |
} |
anyway of getting this to use server time? or a specific timezone actually... like EST
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Unfortunately it doesn't work for me. No matter where I place the code.