- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 Articles
- 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
- How can we create a MySQL function to find out the duration of years, months, days, hours, minutes and seconds?
- Hours and minutes from number of seconds using JavaScript
- Add the following:2 hours 13 minutes 40 seconds and 4 hours 23 minutes 35 seconds.
- Subtract 4 hours 20 minutes 36 seconds from 8 hours 18 minutes 20 seconds.
- Convert 7290 seconds into hours and minutes.
- 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?
- Convert:a. 60 hours into days and hoursb. 50 months into years and months
- Is 3 hours : 150 minutes and 42 days : 5 weeks in the form of proportion?
- Add 24 days 19 hours and 26 days 18 hours.

Advertisements