const target = new Date(location.hash.substr(1)); const second = 1000; const minute = second*60; const hour = minute*60; const day = hour*24; const update_countdown = () => { 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); Object.keys(timeleft).forEach((unit) => { // retrieve value, cast it to string and optionally pad it with a leading 0 const value = timeleft[unit] .toString() .padStart(2, '0'); document.getElementById(unit).innerHTML = value; }); }; update_countdown(); setInterval(update_countdown, 500);