Calculate the Time Difference Between Two Time Strings in Python

Lakshay Kapoor Oct 10, 2023
  1. Use datetime.strptime() to Calculate Time Difference Between Two Time Strings in Python
  2. Use time.sleep() to Calculate the Time Difference Between Two Time Strings in Python
  3. Use datetime.timedelta() to Calculate the Time Difference Between Two Time Strings in Python
Calculate the Time Difference Between Two Time Strings in Python

There are times when we have to deal with date and time-related problems in programming. In Python, data and time are not data types themselves. Still, Python provides a vast range of functions as well as libraries that help in dealing with such problems. One of the date and time-related problems is calculating the time interval between two-time strings.

This tutorial demonstrates different ways of calculating the time interval between two strings in Python.

Use datetime.strptime() to Calculate Time Difference Between Two Time Strings in Python

The datatime class provides a user with a number of functions to deal with dates and times in Python. The strptime() function is used to parse a string value to represent time based on a given format. The string value and time format are stored as the function argument.

Here’s an example program:

time_1 = datetime.strptime("05:00:00", "%H:%M:%S")
time_2 = datetime.strptime("10:00:00", "%H:%M:%S")

time_interval = time_2 - time_1
print(time_difference)

Output:

5:00:00

Here, two time strings are stored in two variables using the datetime.strptime() function. Note that %H, %M and %S are the representatives of Hours, Minutes and, Seconds. After the two time strings are stored with their time format, the time interval between the two is calculated by simply subtracting the two variables.

Use time.sleep() to Calculate the Time Difference Between Two Time Strings in Python

There is a module known as the time module in Python, which helps in printing time in the form of objects, numbers, and strings. It also offers many functions to carry out tasks like measuring time and measuring code efficiency.

One of the functions in Python’s time module is the sleep() function. This function suspends the execution of the present code-block for a certain amount of time mentioned by the user only.

Look at this example code:

import time

time_1 = time.time()

time.sleep(20)

time_2 = time.time()
time_interval = time_2 - time_1
print(time_interval)

Output:

20.005916118621826

Note that in the above code, the time() function from the time module is also used. This function helps in returning the number of seconds passed since the epoch, January 1, 1970, 00:00:00 at UTC. The variable 20 in the argument of the sleep() function represents 20 seconds. In this code, the two time values are taken since the epoch, and in between them, the code stops executing for 20 seconds.

Use datetime.timedelta() to Calculate the Time Difference Between Two Time Strings in Python

There is one more Python module known as the datetime module. This module also provides many classes and functions to deal with dates, times, and time intervals.

The timedelta() class is one of the functions of the datetime module. It’s used to represent a specific time duration or the difference between two dates and times. The functions hold many arguments like days, milliseconds, microseconds, seconds, minutes, hours, and also weeks.

The user can mention these arguments according to the need of the program. Check out an example program here:

import datetime

time_1 = datetime.timedelta(hours=10, minutes=20, seconds=30)
time_2 = datetime.timedelta(hours=20, minutes=30, seconds=45)
print(time_2 - time_1)

Note that in the above code, not all arguments are mentioned in the timedelta class.

Lakshay Kapoor avatar Lakshay Kapoor avatar

Lakshay Kapoor is a final year B.Tech Computer Science student at Amity University Noida. He is familiar with programming languages and their real-world applications (Python/R/C++). Deeply interested in the area of Data Sciences and Machine Learning.

LinkedIn

Related Article - Python DateTime