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