Find the gap between two times in Python

In this tutorial, we will learn how to find a gap between two times in Python.

The gap between two given time

In python, the datetime library provides us with various classes and functions to manipulate with date and time. We will be using some functions of this library for obtaining a gap between two times.

First of all, we will import the timedelta function from the datetime library, this function’s main functionality is to find out the difference between two times or date. Then we will be asking the user to input the two times in 24 hours format.

from datetime import timedelta
print("Enter the time in 24 hrs format\n")
print("enter the first time\n")
hour1 = int(input("hours: "))
min1 = int(input("min: "))
sec1 = int(input("sec: "))
print("enter the second time: ")
hour2 = int(input("hours: "))
min2 = int(input("min: "))
sec2 = int(input("sec: "))

After this, we will just convert the given user input into seconds only and then we will find out the difference between the two times firstly only in terms of seconds and then in time format using timedelta function.

time1 = hour1*60*60 + min1*60 + sec1
time2 = hour2*60*60 + min2*60 + sec2

difference = abs(time1 - time2)

print(f"difference in seconds {difference}\n")
print(f"difference in time format {timedelta(seconds= difference)}")

OUTPUT

Enter the time in 24 hrs format

enter the first time

hours: 9
min: 10
sec: 45
enter the second time: 
hours: 6
min: 25
sec: 30
difference in seconds 9915

difference in time format 2:45:15

Gap between the current time and a given time

For this, we will be using now() function of datetime to obtain the current time and then find out the difference between the current time and the given time.

from datetime import timedelta, datetime

hour1 = int(datetime.now().hour)
min1 = int(datetime.now().minute)
sec1 = int(datetime.now().second)
print("Enter the time in 24 hrs format\n")
hour2 = int(input("hours: "))
min2 = int(input("min: "))
sec2 = int(input("sec: "))

time1 = hour1*60*60 + min1*60 + sec1
time2 = hour2*60*60 + min2*60 + sec2

difference = abs(time1 - time2)

print(f"difference in seconds {difference}\n")
print(f"difference in time format {timedelta(seconds= difference)}")

Here we are storing current hour, minute and seconds in hour1, min1, sec1 variable respectively by using the now() function as shown above.

Output

Enter the time in 24 hrs format

hours: 15
min: 44
sec: 4
difference in seconds 34

difference in time format 0:00:34

Time Difference with AM and PM in consideration

For this first, we will convert the 12-hour time format into the 24-hour format.

def convert_time(time):
    if time[-2:] == "AM" and time[:2] == "12":
        return "00" + time[2:-2]
    elif time[-2:] == "AM":
        return time[:-2]

    elif time[-2:] == "PM" and time[:2] == "12":
        return time[:-2]

    else:
        return str(int(time[:2]) + 12) + time[2:8]


if __name__ == '__main__':
    time_1 = str(input('enter the first time in am/pm format\n'))
    time_2 = str(input('enter the second time in am/pm format\n'))

    time_1_converted = convert_time(time_1)
    time_2_converted = convert_time(time_2)

Here in the convert_time function, we are splitting the string time input given by the user and check whether they have given AM or PM as the input and then convert the input time accordingly. After that, we will separate the hours, minutes and seconds into different variables and then just follow the above program.

hour1 = time_1_converted[:2]
min1 = time_1_converted[3:-4]
sec1 = time_1_converted[6:]

hour2 = time_2_converted[:2]
min2 = time_2_converted[3:-4]
sec2 = time_2_converted[6:]
time1 = int(hour1) * 60 * 60 + int(min1) * 60 + int(sec1)
time2 = int(hour2) * 60 * 60 + int(min2) * 60 + int(sec2)
difference = abs(time1 - time2)

print(f"difference in seconds {difference}\n")
print(f"difference in time format {timedelta(seconds= difference)}")

Output

enter the first time in am/pm format
01:25:00 AM
enter the second time in am/pm format
01:25:00 PM
difference in seconds 41820

difference in time format 11:37:00

If this post was useful then please leave a comment and share this with your friends.

Leave a Reply

Your email address will not be published.