I’m trying to create a program that takes the difference of two military times and get its time difference.
Example:
**AM to PM**
Time 1: 0900
Time 2: 1730
Time Difference: 8 hours 30 minutes
**PM to AM**
Time 1: 1200
Time 2: 1100
Time Difference: 23 hours 0 minutes
Using a couple of if than else statements, I was able to figure out how to convert from military hours into standard hours but I’m stuck with how to go about subtracting time. I was trying to come up with a way to do with on paper just with addition and subtraction but I haven’t developed a method that works in all cases. Any help?
One option might be to split the time into two elements, the hours and the minutes.
First, take the time, ie. 1730 and divide by 100. If its an integer or similar, it should result in 17 hours (it will automatically round down).
Then take 1730 and mod it by 100 to get 30 minutes.
If you’re unfamiliar with modulus (%), it just returns the remainder after dividing the two numbers, so 7 % 3 would be 1.