Today, I received a letter from a student asking for an algorithm to calculate the number of hours between two points, along with a request: 30 minutes per 0.5 hours, such as 25 minutes 0, 45 minutes can be counted 0.5 hours. This application is particularly useful in calculating overtime work and is now shared as follows:
Suppose column A has the start time, column B is the end time, and the number of overtime hours in column C.
Start time |
End time |
Hours of overtime |
8:00 |
16:00 |
8 |
8:00 |
15:30 |
7.5 |
16:00 |
0:00 |
8 |
16:00 |
23:30 |
7.5 |
0:00 |
8:00 |
8 |
0:00 |
7:30 |
7.5 |
19:30 |
7:30 |
12 |
19:30 |
7:00 |
11.5 |
16:00 |
16:25 |
0 |
16:00 |
16:30 |
0.5 |
16:00 |
16:45 |
0.5 |
The specific formula is as follows: =rounddown (ROUND (B2-A2) *24*60,0)/30,0)/2
Explanation: Subtract time from time, the result is a decimal in days, so multiply it by 24 hours and multiply by 60, convert to minutes, and, in 30 minutes, perform an unconditional position operation RoundDown (), and find a few 30 minutes, and then divide by 2 to convert to hours. Because of the precision of the decimal point, the above method to find the data may be less than 0.5 hours, so add a rounding function round (), to ensure that the decimal part of the error.