const second = 1000; const minute = second*60; const hour = minute*60; const day = hour*24; const countdown = (target) => { 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'); }); return ` ${formatted[0]} Days
${formatted[1]} Hours
${formatted[2]} Minutes
${formatted[3]} Seconds
`; }; const hash_date = new Date(location.hash.substr(1)) const update_all = () => { document.getElementById('output').innerHTML = countdown(hash_date); } update_all(); setInterval(update_all, 500);