Days seems to be the trickiest calculation, otherwise it's pretty straight-forward. Subtract the current milliseconds from the target milliseconds to get the duration in milliseconds. Then for each value but days, take the floor of the duration divided by the number of milliseconds in either a year, month, hour, minute or second. This gives you the number or years, months, hours, minutes or seconds in the duration. Finally, take the modulus of each of the values.
For days, subtract the number of years and months in milliseconds from the duration to get the remaining milliseconds, then take the floor of the remaining milliseconds divided by the number of milliseconds in a day.
function countdown(targetDate) {
var nowMillis = new Date().getTime();
var targetMillis = targetDate.getTime();
var duration = targetMillis - nowMillis;
var years = Math.floor(duration / 3.154e+10);
var durationMinusYears = duration - (years * 3.154e+10);
var months = Math.floor(duration / 2.628e+9) % 12;
var durationMinusMonths = durationMinusYears - (months * 2.628e+9);
var days = Math.floor(durationMinusMonths / 8.64e+7);
var hours = Math.floor(duration / 3.6e+6 ) % 24;
var mins = Math.floor(duration / 60000 ) % 60;
var secs = Math.floor(duration / 1000 ) % 60;
return [ years, months, days, hours, mins, secs ];
}
console.log('Count down until IE11 is no longer supported => ' + countdown(new Date(2020, 9, 13, 0, 0)));