

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
Converting seconds in years days hours and minutes in JavaScript
Problem
We are required to write a JavaScript function that takes in a number, num, that represents the number of seconds as the first and the only argument.
Our function should then construct and return a string that contains information of years, days, hours and minutes contained in those seconds, obviously if contained at all.
For the purpose of this question, we consider that all the years have 365 days
For example, if the input to the function is −
const num = 5454776657;
Then the output should be −
const output = '172 years, 353 days, 23 hours, 44 minutes and 17 seconds';
Example
Following is the code −
const findTime = (num) => { if(num < 1){ return '0' }; const qualifier = num => (num > 1 ? 's' : '') const numToStr = (num, unit) => num > 0 ? ` ${num} ${unit}${qualifier(num)}` : '' const oneMinute = 60; const oneHour = oneMinute * 60; const oneDay = oneHour * 24; const oneYear = oneDay * 365; const times = { year: ~~(num / oneYear), day: ~~((num % oneYear) / oneDay), hour: ~~((num % oneDay) / oneHour), minute: ~~((num % oneHour) / oneMinute), second: num % oneMinute, }; let str = ''; for (let [key, value] of Object.entries(times)) { str += numToStr(times[key], key) } const arr = str.trim().split(' ') const res = [] arr.forEach((x, i) => { if (i % 2 === 0 && i !== 0) res.push(i === arr.length - 2 ? 'and' : ',') res.push(x) }) return res.join(' ').replace(/\s,/g, ',') } console.log(findTime(5454776657));
Output
Following is the console output −
172 years, 353 days, 23 hours, 44 minutes and 17 seconds
- Related Questions & Answers
- Converting seconds into days, hours, minutes and seconds in C++
- C++ Program for converting hours into minutes and seconds
- MySQL DateTime Now()+5 days/hours/minutes/seconds?
- Converting days into years months and weeks - JavaScript
- Hours and minutes from number of seconds using JavaScript
- How can we create a MySQL function to find out the duration of years, months, days, hours, minutes and seconds?
- Python program to convert seconds into hours, minutes and seconds
- MySQL update datetime column values and add 10 years 3 months 22 days and 10 hours, 30 minutes to existing data?
- How to convert JavaScript seconds to minutes and seconds?
- How to add hours and minutes to a date with JavaScript?
- Program to convert given number of days in terms of Years, Weeks and Days in C
- How to get days, months and years between two Java LocalDate?
- How to get the seconds and minutes between two Instant timestamps in Java
- How to convert a MySQL TIME value to days and hours form?
- Change leaves duration from hours to days in SAP Fiori app
Advertisements