Time(days, hours, min, sec) Mod

Sep 7, 2019 at 3:14am
I have another code I need to write and I have it completed just about, but I need a thorough explanation, please. Why is this calculation as follows?

days = input_seconds / 60 / 60 / 24;
hours = (input_seconds / 60 / 60) % 24;
minutes = (input_seconds / 60) % 60;
seconds = input_seconds % 60;


Can someone help me understand the reasoning behind this formula? This is not mine, I found this on this forum and would like an explanation. Thank you very much!
Sep 7, 2019 at 3:30am
Well think about a simpler example of say

number = 234;

and you want to extract
hundreds = 2;
tens = 3;
units = 4;

You use a series of / 10 and % 10 operations to do that.

Or replace all those magic numbers with meaningful constants.
days = input_seconds / SECONDS_PER_MINUTE / MINUTES_PER_HOUR / HOURS_PER_DAY;
hours = (input_seconds / SECONDS_PER_MINUTE / MINUTES_PER_HOUR) % HOURS_PER_DAY;
minutes = (input_seconds / SECONDS_PER_MINUTE) % MINUTES_PER_HOUR;
seconds = input_seconds % SECONDS_PER_MINUTE;
Sep 7, 2019 at 3:34am
Thank you again Salem, you're a great help!!!!
Topic archived. No new replies allowed.