Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 −
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)"
Advertisements