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

217
Views
Is my modulo use wrong or is there a Math.floor rounding error happening with my seconds to minutes calculation?

SOLVED: Seems it's a problem that only exists in Codepen. Running code in a different environment didn't reproduce the problem.


Am writing code to split seconds into years, days, minutes, seconds. I'm getting some unexpected results: eg 3600 seconds comes out as 1 hour. But 3599 seconds comes out as 60 minutes and 59 seconds.

Firstly, there should never be 60 minutes. This should be 1 hour. But that aside the actual result is wrong.

Is there some error in my calculation logic, or is there a funky rounding error going on using modulo and Math.floor? Thanks!

  function convertFromSeconds(time) {

  const minute = 60
  const hour = 60 * minute; //3600
  const day = 24 * hour; // 86400
  const year = 365 * day //31536000
  
  const years = Math.floor(time / year)
  const days = Math.floor((time % year) / day)
  const hours = Math.floor((time % day) / hour)
  const minutes = Math.floor((time % hour) / minute)
  const seconds = time % minute;

  // Rest of code formats the above result into a string for output
}

function convertFromSeconds(time) {

  const minute = 60
  const hour = 60 * minute; //3600
  const day = 24 * hour; // 86400
  const year = 365 * day //31536000

  const years = Math.floor(time / year)
  const days = Math.floor((time % year) / day)
  const hours = Math.floor((time % day) / hour)
  const minutes = Math.floor((time % hour) / minute)
  const seconds = time % minute;

  // Rest of code formats the above result into a string for output

  return { years, days, hours, minutes, seconds }
}

console.log('3600:', convertFromSeconds(3600))
console.log('3599:', convertFromSeconds(3599))

about 4 years ago · Juan Pablo Isaza
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!