Given two times `h1:m1`

and `h2:m2`

denoting hours and minutes in 24 hours clock format. The current clock time is given by `h1:m1`

. The task is to calculate the difference between two times in minutes and print the difference between two times in `h:m`

format.

**Examples:**

Input :h1=7, m1=20, h2=9, m2=45

Output :2 : 25

The current time is 7 : 20 and given time is 9 : 45.

The difference between them is 145 minutes.

The result is 2 : 25 after converting to h : m format.

Input :h1=15, m1=23, h2=18, m2=54

Output :3 : 31

The current time is 15 : 23 and given time is 18 : 54.

The difference between them is 211 minutes.

The result is 3 : 31 after converting to h : m format.

Input :h1=16, m1=20, h2=16, m2=20

Output :Both times are same

The current time is 16 : 20 and given time is also 16 : 20.

The difference between them is 0 minutes.

As the difference is 0, we are printing “Both are same times”.

**Approach:**

- convert both the times into minutes
- find the difference in minutes
- if difference is 0, print “Both are same times”
- else convert difference into h : m format and print

Below is the implementation.

`# Python program to find the ` `# difference between two times ` ` ` ` ` `# function to obtain the time ` `# in minutes form ` `def` `difference(h1, m1, h2, m2): ` ` ` ` ` `# convert h1 : m1 into ` ` ` `# minutes ` ` ` `t1 ` `=` `h1 ` `*` `60` `+` `m1 ` ` ` ` ` `# convert h2 : m2 into ` ` ` `# minutes ` ` ` `t2 ` `=` `h2 ` `*` `60` `+` `m2 ` ` ` ` ` `if` `(t1 ` `=` `=` `t2): ` ` ` `print` `(` `"Both are same times"` `) ` ` ` `return` ` ` `else` `: ` ` ` ` ` `# calculating the difference ` ` ` `diff ` `=` `t2` `-` `t1 ` ` ` ` ` `# calculating hours from ` ` ` `# difference ` ` ` `h ` `=` `(` `int` `(diff ` `/` `60` `)) ` `%` `24` ` ` ` ` `# calculating minutes from ` ` ` `# difference ` ` ` `m ` `=` `diff ` `%` `60` ` ` ` ` `print` `(h, ` `":"` `, m) ` ` ` `# Driver's code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `difference(` `7` `, ` `20` `, ` `9` `, ` `45` `) ` ` ` `difference(` `15` `, ` `23` `, ` `18` `, ` `54` `) ` ` ` `difference(` `16` `, ` `20` `, ` `16` `, ` `20` `) ` ` ` `# This code is contributed by SrujayReddy ` |

*chevron_right*

*filter_none*

**Output:**

2 : 25 3 : 31 Both are same times

Attention geek! Strengthen your foundations with the **Python Programming Foundation** Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the **Python DS** Course.

## Recommended Posts:

- Python program to get Current Time
- Get Current Date and Time using Python
- Get current time in milliseconds using Python
- Get Current Time in different Timezone using Python
- Python VLC MediaPlayer - Getting Current media time
- How to add hours to the current time in Python?
- Python | Find current weather of any city using openweathermap API
- How to find the current capacity of a list in Python
- Python | Create an empty text file with current date as its name
- Get directory of current Python script
- Get parent of current directory using Python
- Change current working directory with Python
- Get current date using Python
- Get current Gold price using Python
- Get current timestamp using Python
- Python - Elements Maximum till current index in List
- Python - Get Today's Current Day using Speech Recognition
- Python VLC MediaPlayer - Getting length of current media
- Python VLC MediaPlayer - Getting Current State
- Python VLC MediaPlayer – Getting Current Subtitle

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.