+5

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

c++

1/24/2017 11:23:06 PM

Kyle Martin

31 Answers

New Answer

+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);

+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;

+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^^

+2

Minutes = Seconds * 60 Hours = Minutes * 60 Hours = Seconds * 60 * 60 Hours = Seconds * 3600 Days = Hours * 24 Days = Seconds * 3600 * 24 Days = Seconds * 86400

+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?

+1

to convert seconds to minutes ÷ seconds by 60, minutez to hours ÷ minutes by 60, hours to days divide hours by 24! SIMPLE

+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...

+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.

0

I shouldn't use a modulus to get rid of the decimal?

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)?

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

0

for my code it keeps giving me a decimal

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;

0

won't it be truncated though?

0

What output do you want? Let's say you have 3700 seconds, what should the output look like?

0

how many hours for those seconds

0

without decimals. I need an exact answer

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...

0

I know. I'm trying to get it using a math equation

0

like 65 days 1 hour 37 minutes 67 seconds