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

104
Views
How to convert seconds to biggest significative time unit

What would be the cleanest way to convert an amount of seconds to the biggest unit that is not 0 and then display only the significative unit.

Input -> Expected output
1 -> 1s
61 -> 1m
120 -> 2m
3620 -> 1h
86400 -> 1day
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

const units = [
  [1, "s"],
  [60, "m"],
  [60 * 60, "h"],
  [60 * 60 * 24, "day"]
];

function displayTime(seconds) {
  let bestUnit = units[0];
  for(const unit of units) {
    if(seconds >= unit[0]) {
        bestUnit = unit;
    }
  }
  const [divisor, label] = bestUnit;
  return Math.floor(seconds /divisor) + label;
}

console.log(displayTime(1)); // 1s
console.log(displayTime(61)); // 1m
console.log(displayTime(120)); // 2m
console.log(displayTime(3620)); // 1h
console.log(displayTime(86400)); // 1day

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