function countdown(targetDate) { var nowMillis = new Date().getTime(); var targetMillis = targetDate.getTime(); var diff = targetMillis - nowMillis; var years = Math.floor(diff / 3.154e+10); var diffMinusYears = diff - (years * 3.154e+10); var months = Math.floor(diff / 2.628e+9) % 12; var diffMinusMonths = diffMinusYears - (months * 2.628e+9); var days = Math.floor(diffMinusMonths / 8.64e+7); var hours = Math.floor(diff / 3.6e+6 ) % 24; var mins = Math.floor(diff / 60000 ) % 60; var secs = Math.floor(diff / 1000 ) % 60; return [ years, months, days, hours, mins, secs ]; } var target = new Date(2020, 0, 31, 0, 0); window.setInterval(function() { console.log(countdown(target)); }, 1000);