Hours and minutes from number of seconds using JavaScript


Problem

We are required to write a JavaScript function that takes in the number of second and return the number of hours and number of minutes contained in those seconds.

Input

const seconds = 3601;

Output

const output = "1 hour(s) and 0 minute(s)";

Example

Following is the code −

 Live Demo

const seconds = 3601;
const toTime = (seconds = 60) => {
   const hR = 3600;
   const mR = 60;
   let h = parseInt(seconds / hR);
   let m = parseInt((seconds - (h * 3600)) / mR);
   let res = '';
   res += (`${h} hour(s) and ${m} minute(s)`)
   return res;
};
console.log(toTime(seconds));

Output

"1 hour(s) and 0 minute(s)"

Updated on: 17-Apr-2021

288 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements