Skip to content

Instantly share code, notes, and snippets.

@Dan-Q Dan-Q/countdown.html
Last active Sep 3, 2019

Embed
What would you like to do?
Workday countdown timer
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Countdown</title>
<style type="text/css">
body {
margin: 0;
font-family: sans-serif;
font-size: 8vh;
color: white;
background: black;
}
main {
display: flex;
flex-direction: column;
width: 100vw;
height: 100vh;
justify-content: center;
align-items: center;
}
main > div:first-child {
margin-bottom: 4vh;
font-weight: bold;
}
</style>
</head><body>
<main></main>
<aside></aside>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
<script>
'use strict';
const main = document.querySelector('main'), aside = document.querySelector('aside');
const dates = ['2019-08-20', '2019-08-21', '2019-08-23',
'2019-09-03', '2019-09-04', '2019-09-06',
'2019-09-10', '2019-09-11', '2019-09-13',
'2019-09-17', '2019-09-18', '2019-09-20',
'2019-09-24', '2019-09-25', '2019-09-27',
'2019-10-01', '2019-10-02', '2019-10-04',
'2019-10-08', '2019-10-09', '2019-10-11',
'2019-10-15', '2019-10-16'];
const startHour = '09:00';
const hoursPerDay = 8;
const secondsPerDay = hoursPerDay * 3600;
function pluraliseAsNeeded(num, unit){
// NOTE: lazy-pluralises, which is fine for hour(s)/minute(s)/second(s)
switch(num){
case 0: return '';
case 1: return `1 ${unit}`;
default: return `${num} ${unit}s`;
}
}
setInterval(function(){
const now = moment();
const today = now.format('YYYY-MM-DD');
const daysIncludingToday = dates.filter(date=>date>=today);
const isAWorkDay = dates.includes(today);
const numDaysIncludingToday = daysIncludingToday.length;
const hoursRemainingAfterToday = hoursPerDay * (numDaysIncludingToday - 1);
const hoursRemainingAtStartOfToday = hoursPerDay * numDaysIncludingToday;
const startOfToday = moment(`${today} ${startHour}`);
const secondsPassedSinceStartOfToday = now.diff(startOfToday, 'seconds');
const workSecondsPassedSinceStartOfToday = isAWorkDay ? (secondsPassedSinceStartOfToday < 0 ? 0 : (secondsPassedSinceStartOfToday > secondsPerDay ? secondsPerDay : secondsPassedSinceStartOfToday)) : 0;
const workSecondsRemainingToday = secondsPerDay - workSecondsPassedSinceStartOfToday;
const workSecondsRemaining = workSecondsRemainingToday + (numDaysIncludingToday - 1) * secondsPerDay;
const wholeHoursRemaining = Math.floor(workSecondsRemaining / 3600);
const wholeMinutesRemaining = Math.floor((workSecondsRemaining - (wholeHoursRemaining * 3600)) / 60);
const wholeSecondsRemaining = Math.floor((workSecondsRemaining - (wholeHoursRemaining * 3600) - (wholeMinutesRemaining * 60)));
const resultParts = [pluraliseAsNeeded(wholeHoursRemaining, 'hour'), pluraliseAsNeeded(wholeMinutesRemaining, 'minute'), pluraliseAsNeeded(wholeSecondsRemaining, 'second')];
const resultHTML = resultParts.map(part=>`<div>${part}</div>`).join('');
main.innerHTML = workSecondsRemaining <= 0 ? 'Dan has left the building!' : `<div>Dan will be gone in</div>${resultHTML}`;
}, 1000);
</script>
</body></html>
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <http://unlicense.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.