Table of Contents
Introduction
Our task is to calculate the difference between current time given in h1:m1 and given time given in h2:m2 where, h1, h2 and m1,m2 are hours and minutes in 24 hour format.
Program
def time_diff(h1, m1, h2, m2): print("The given times are: ", h1, ":", m1) time1 = h1 * 60 + m1 print("The given times are:", h2, ":", m2) time2 = h2 * 60 + m2 if(time1 == time2): print("Both are same, there is no difference!") return else: difference = time2 - time1 h = (int(difference/60)) % 24 m = difference % 60 print("The difference in times", h, ":", m, "\n") time_diff(9, 10, 11, 45) time_diff(5, 5, 5, 5)
Output
Explanation
In the above python code our approach is,
- We have converted the given times into minutes and found the difference in minutes.
- If the difference is there, we have converted the difference value into hours and minutes and returned the value.
- If there is no difference i.e. 0, we return the same value with text no difference!
0 Comments