# Difference between two given times

Given two times in string “HH:MM” format. Here ‘H’ shows hours and ‘M’ shows minutes. You have to find the difference in the same string format between these two strings. But both given strings should follow these cases.

1) Time 1 will be less than or equal to time2

2) Hours will be possible from 0 to 23.

3) Minutes will be possible from 0 to 59

Examples:

Input : s1 = "14:00"; s2 = "16:45"; Output : result = "2:45" Input : s1 = "1:04" s2 = "13:05" Output : result = "12:01"

The idea is to first remove colon. After removing colon, hour difference can be computed using simple mathematics.

## C++

`// C++ program to find difference between two ` `// given times. ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// remove ':' and convert it into an integer ` `int` `removeColon(string s) ` `{ ` ` ` `if` `(s.size() == 4) ` ` ` `s.replace(1, 1, ` `""` `); ` ` ` ` ` `if` `(s.size() == 5) ` ` ` `s.replace(2, 1, ` `""` `); ` ` ` ` ` `return` `stoi(s); ` `} ` ` ` `// Main function which finds difference ` `string diff(string s1, string s2) ` `{ ` ` ` ` ` `// change string (eg. 2:21 --> 221, 00:23 --> 23) ` ` ` `int` `time1 = removeColon(s1); ` ` ` `int` `time2 = removeColon(s2); ` ` ` ` ` `// difference between hours ` ` ` `int` `hourDiff = time2 / 100 - time1 / 100 - 1; ` ` ` ` ` `// difference between minutes ` ` ` `int` `minDiff = time2 % 100 + (60 - time1 % 100); ` ` ` ` ` `if` `(minDiff >= 60) { ` ` ` `hourDiff++; ` ` ` `minDiff = minDiff - 60; ` ` ` `} ` ` ` ` ` `// convert answer again in string with ':' ` ` ` `string res = to_string(hourDiff) + ` `':'` `+ to_string(minDiff); ` ` ` `return` `res; ` `} ` ` ` `// Driver program to test above functions ` `int` `main() ` `{ ` ` ` `string s1 = ` `"14:00"` `; ` ` ` `string s2 = ` `"16:45"` `; ` ` ` `cout << diff(s1, s2) << endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python 3 program to find difference between two ` `# given times. ` ` ` `# Remove ':' and convert it into an integer ` `def` `removeColon(s): ` ` ` ` ` `if` `(` `len` `(s) ` `=` `=` `4` `): ` ` ` `s ` `=` `s[:` `1` `] ` `+` `s[` `2` `:] ` ` ` ` ` `if` `(` `len` `(s) ` `=` `=` `5` `): ` ` ` `s ` `=` `s[:` `2` `] ` `+` `s[` `3` `:] ` ` ` ` ` `return` `int` `(s) ` ` ` `# Main function which finds difference ` `def` `diff(s1, s2): ` ` ` ` ` `# Change string ` ` ` `# (eg. 2:21 --> 221, 00:23 --> 23) ` ` ` `time1 ` `=` `removeColon(s1) ` ` ` `time2 ` `=` `removeColon(s2) ` ` ` ` ` `# Difference between hours ` ` ` `hourDiff ` `=` `time2 ` `/` `/` `100` `-` `time1 ` `/` `/` `100` `-` `1` `; ` ` ` ` ` `# Difference between minutes ` ` ` `minDiff ` `=` `time2 ` `%` `100` `+` `(` `60` `-` `time1 ` `%` `100` `) ` ` ` ` ` `if` `(minDiff >` `=` `60` `): ` ` ` `hourDiff ` `+` `=` `1` ` ` `minDiff ` `=` `minDiff ` `-` `60` ` ` ` ` `# Convert answer again in string with ':' ` ` ` `res ` `=` `str` `(hourDiff) ` `+` `':'` `+` `str` `(minDiff) ` ` ` ` ` `return` `res ` ` ` `# Driver code ` `s1 ` `=` `"14:00"` `s2 ` `=` `"16:45"` ` ` `print` `(diff(s1, s2)) ` ` ` `# This code is contributed by ApurvaRaj ` |

*chevron_right*

*filter_none*

Output:

2:45

This article is contributed by **Harshit Agrawal**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- Times required by Simple interest for the Principal to become Y times itself
- Sum of all numbers formed having 4 atmost X times, 5 atmost Y times and 6 atmost Z times
- Find the value of N XOR'ed to itself K times
- Find the value of XXXX.....(N times) % M where N is large
- Count of subarrays which contains a given number exactly K times
- GCD of two numbers formed by n repeating x and y times
- Probability of getting a sum on throwing 2 Dices N times
- Convert a number of length N such that it contains any one digit at least 'K' times
- Find the number of times every day occurs in a month
- Check if X and Y can be made zero by using given operation any number of times
- Count different numbers possible using all the digits their frequency times
- Find Nth smallest number that is divisible by 100 exactly K times
- Maximum number of dots after throwing a dice N times
- Maximum profit by buying and selling a share at most k times
- Number formed by adding product of its max and min digit K times
- Maximum inversions in a sequence of 1 to N after performing given operations at most K times
- Smallest integer greater than n such that it consists of digit m exactly k times
- Check if original Array is retained after performing XOR with M exactly K times
- GCD of elements occurring Fibonacci number of times in an Array
- Find the K-th minimum element from an array concatenated M times