Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

175
Views
How could have i done this more efficiently?
    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 );

Is there any easier way to do this part above.I tried subtracting two dates but it gives me wrong year.I have tried this

      new Date(date1 - date2);

But it gives me 2024 i know it isn't posible to have date before 1970 but is there any other way this could be done?

      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 );
about 4 years ago · Santiago Gelvez
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!