You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

49 lines
1.3 KiB
JavaScript

const second = 1000;
const minute = second*60;
const hour = minute*60;
const day = hour*24;
const countdown = (target, heading) => {
const now = new Date();
const diff = target - now;
const timeleft = {};
timeleft.days = Math.trunc(diff/day);
timeleft.hours = Math.trunc(diff%day/hour);
timeleft.minutes = Math.trunc(diff%day%hour/minute);
timeleft.seconds = Math.trunc(diff%day%hour%minute/second);
const formatted = Object.keys(timeleft).map(unit => {
// retrieve value, cast it to string and optionally pad it with a leading 0
return timeleft[unit]
.toString()
.padStart(2, '0');
});
if (heading === 1 || formatted.filter(timespan => timespan.substr(0, 1) == '-').length === 0) {
return `
<h${heading}>${formatted[0]} Days</h${heading}>
<h${heading}>${formatted[1]} Hours</h${heading}>
<h${heading}>${formatted[2]} Minutes</h${heading}>
<h${heading}>${formatted[3]} Seconds</h${heading}>
`;
}
};
const hash_date = new Date(location.hash.substr(1));
const update_all = () => {
let i = 0;
const curDate = new Date();
document.body.innerHTML = location
.hash
.split('#')
.slice(1)
.map(fragment => new Date(fragment))
.sort((a, b) => b - a)
.map(date => countdown(date, ++i))
.join('');
}
update_all();
setInterval(update_all, 500);