Hours and minutes from number of seconds using JavaScript

JavascriptWeb DevelopmentFront End Technology

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)"
raja
Published on 17-Apr-2021 11:47:16
Advertisements