+ 5

How to convert seconds to minutes, hours, and days?

24th Jan 2017, 11:23 PM
Kyle Martin
Kyle Martin - avatar
31 Réponses
+ 16
Not for the accurate result. If you want to split the seconds into the above you can use this: int sec = 3700, mins, hours, days; sec -= (60 * 60 * 24) * (hours = sec / (60 * 60 * 24)); sec -= (60 * 60) * (days = sec / (60 * 60)); sec -= 60 * (mins = sec / 60);
24th Jan 2017, 11:43 PM
Robobrine
Robobrine - avatar
+ 8
unsigned int d=sec/86400; sec=sec%86400; unsigned int h=sec/3600; sec=sec%3600; unsigned int m=sec/60; sec=sec%60;
9th Mar 2017, 11:38 AM
Pranay
Pranay - avatar
+ 5
It would be 1 hour 1 minute and 40 seconds (cmon, basic math xD), but whatever. You know I gave you the complete code for this 10 comments ago? Well, here it is again: int input, sec, mins, hours, days; cout << "Enter a number:" << endl; cin >> input; sec = input; sec -= (60 * 60 * 24) * (hours = sec / (60 * 60 * 24)); sec -= (60 * 60) * (days = sec / (60 * 60)); sec -= 60 * (mins = sec / 60); cout << input << " seconds are " << days << " day(s) " << hours << " hour(s) " << mins << " minute(s) and " << sec << " second(s)" << endl; And don't say you ONLY want hours if clearly you also want it to display minutes seconds and days at the same time^^
25th Jan 2017, 12:12 AM
Robobrine
Robobrine - avatar
+ 2
Minutes = Seconds * 60 Hours = Minutes * 60 Hours = Seconds * 60 * 60 Hours = Seconds * 3600 Days = Hours * 24 Days = Seconds * 3600 * 24 Days = Seconds * 86400
19th Feb 2017, 2:35 PM
Varun Ramani
Varun Ramani - avatar
+ 1
You still didn't answer what the exact output for 3700 seconds should look like. Apparently you don't want it to be 1 hour (rounded) and you also don't want it to be 1.0278 hours (exact, but decimals), so what do you want? 37/36 hours? 10278e-4 hours?
25th Jan 2017, 12:09 AM
Robobrine
Robobrine - avatar
+ 1
to convert seconds to minutes ÷ seconds by 60, minutez to hours ÷ minutes by 60, hours to days divide hours by 24! SIMPLE
8th Feb 2017, 11:21 AM
Mohd Haider
Mohd Haider - avatar
+ 1
int hours = seconds / 3600; int minutes = ((seconds % 3600) / 60); int days = seconds/86400; this will calculate minutes, hours and days from the given seconds...
3rd Mar 2017, 11:36 PM
sourabh Jhamnani
sourabh Jhamnani - avatar
+ 1
https://code.sololearn.com/ct0zZ5GRO138/?ref=app d=secArg/(3600*24); h=(secArg%(24*3600))/3600; m=(secArg%3600)/60; s=(secArg%3600)%60; @Robobrime, i understand Your point of view but i use one operation less excliding assignings and not needed multiplications ofcourse.
5th Mar 2017, 8:50 AM
Michał Bujakowski
Michał Bujakowski - avatar
0
I shouldn't use a modulus to get rid of the decimal?
24th Jan 2017, 11:34 PM
Kyle Martin
Kyle Martin - avatar
0
double sec = 1000; double mins = sec / 60; double hours = sec / (60 * 60); double days = sec / (60 * 60 * 24); Or do you want it mixed (like 3700 seconds are 1 hour 1 minute and 40 seconds)?
24th Jan 2017, 11:41 PM
Robobrine
Robobrine - avatar
0
the code is getting the seconds from the user if over 60 display minutes for how many seconds, if over 3600 display hours fir the seconds and if over 86400 display days for seconds
24th Jan 2017, 11:46 PM
Kyle Martin
Kyle Martin - avatar
0
for my code it keeps giving me a decimal
24th Jan 2017, 11:47 PM
Kyle Martin
Kyle Martin - avatar
0
If you define the seconds, minutes, hours and days as integer you won't have any decimals. int input, sec, mins, hours, days; cout << "Enter a number:" << endl; cin >> input; sec = input; sec -= (60 * 60 * 24) * (hours = sec / (60 * 60 * 24)); sec -= (60 * 60) * (days = sec / (60 * 60)); sec -= 60 * (mins = sec / 60); cout << input << " seconds are " << days << " day(s) " << hours << " hour(s) " << mins << " minute(s) and " << sec << " second(s)" << endl;
24th Jan 2017, 11:52 PM
Robobrine
Robobrine - avatar
0
won't it be truncated though?
24th Jan 2017, 11:53 PM
Kyle Martin
Kyle Martin - avatar
0
What output do you want? Let's say you have 3700 seconds, what should the output look like?
24th Jan 2017, 11:56 PM
Robobrine
Robobrine - avatar
0
how many hours for those seconds
24th Jan 2017, 11:58 PM
Kyle Martin
Kyle Martin - avatar
0
without decimals. I need an exact answer
25th Jan 2017, 12:01 AM
Kyle Martin
Kyle Martin - avatar
0
You can't just only display the hours and then not round the result or have some decimals, that's not how it works...
25th Jan 2017, 12:04 AM
Robobrine
Robobrine - avatar
0
I know. I'm trying to get it using a math equation
25th Jan 2017, 12:04 AM
Kyle Martin
Kyle Martin - avatar
0
like 65 days 1 hour 37 minutes 67 seconds
25th Jan 2017, 12:10 AM
Kyle Martin
Kyle Martin - avatar