const yearTxt = document.getElementById( "years" ); const monthTxt = document.getElementById( "months" ); const daysTxt = document.getElementById( "days" ); const hoursTxt = document.getElementById( "hours" ); const minutesTxt = document.getElementById( "minutes" ); const secondsTxt = document.getElementById( "seconds" ); const countDown = () => { let current = new Date(); let dateOfPotentialDeath = new Date( "2077-05-13T08:05:00.000Z" ); dateOfPotentialDeath.setFullYear( "2077" ); let rest; const totalSeconds = ( dateOfPotentialDeath.getTime() - current.getTime() ) / 1000; const years = Math.floor( totalSeconds / 31_536_000 ); rest = ( totalSeconds / 31536000 ) - years; const months = Math.floor( ( rest * 31_536_000 ) / 2_628_288 ); rest = ( ( rest * 31_536_000 ) / 2_628_288 ) - months; const days = Math.floor( ( rest * 2_628_288 ) / 86400 ); rest = ( ( rest * 2_628_288 ) / 86400 ) - days; const hours = Math.floor( ( rest * 24 ) ); rest = ( rest * 24 ) - hours; const minutes = Math.floor( rest * 60 ); rest = ( rest * 60 ) - minutes; const seconds = Math.floor( rest * 60 ); console.log( years, months, days, rest, hours );¿Hay alguna manera más fácil de hacer esta parte anterior? Intenté restar dos fechas pero me da un año incorrecto. He intentado esto
new Date(date1 - date2);Pero me da 2024, sé que no es posible tener una fecha anterior a 1970, pero ¿hay alguna otra forma de hacerlo?
secondsTxt.innerText = seconds; minutesTxt.innerText = minutes; hoursTxt.innerText = hours; daysTxt.innerText = days; monthTxt.innerText = months; yearTxt.innerText = years; // const days = Math.floor(seconds / 3600 / 60) // const hours = Math.floor(seconds/3600) }; setInterval( countDown, 1000 );