Monday, November 18, 2024
Google search engine
HomeLanguagesCalculate Time Difference in Python

Calculate Time Difference in Python

In this article, we will discuss how to find the gap between two times in Python.

If we want to know the difference at which some particular events has occurred then we should know the time gap between them.

Example:

Input: Start time is : 06:20:50, End time is : 11:56:18
Output: Time difference in seconds is 20128.0 seconds 
Explanation: The time difference between the two times is 20128 seconds.

We will discuss the following topics:

  • Calculate the time difference in hours, minutes and seconds
  • Calculate the time interval between two given times
  • Using the divmod( ) function

Calculate the time difference in hours, minutes and seconds

For finding differences in hours, minutes and seconds we will perform the following actions:

  • We will import daytime from daytime module
  • We will have start and end time of event
  • Then calculate the difference between them
  • And finally calculate the difference in hours, minutes and seconds

Python3




from datetime import datetime
  
start = datetime.strptime("4:25:40", "%H:%M:%S")
end = datetime.strptime("11:40:10", "%H:%M:%S")
  
difference = end - start
  
seconds = difference.total_seconds()
print('difference in seconds is:', seconds)
  
minutes = seconds / 60
print('difference in minutes is:', minutes)
  
hours = seconds / (60 * 60)
print('difference in hours is:', hours)


 Output:

difference in seconds is: 26070.0
difference in minutes is: 434.5
difference in hours is: 7.241666666666666

Calculate the time interval between two given times

Now we will calculate the time difference in seconds between two given times.

We will perform the following actions:

  • Import daytime from daytime module
  • We will have a start time and end time
  • Calculate the difference
  • Finally, calculate the difference in seconds

Python3




from datetime import datetime
  
start = "2:13:57"
end = "11:46:38"
  
  
time1 = datetime.strptime("6:20:50", "%H:%M:%S")
print('Start time is :', time1.time())
  
time2 = datetime.strptime("11:56:18", "%H:%M:%S")
print('End time is :', time2.time())
  
  
delta = time2 - time1
  
  
print("Time difference in seconds is", delta.total_seconds(), "seconds")


Output:

Start time is : 06:20:50
End time is : 11:56:18
Time difference in seconds is 20128.0 seconds

Using the divmod( ) function

We can get a more precise time difference using the divmod( ) function. Using this function get the difference in a number of days also.

Python3




import datetime
    
time1 = datetime.datetime(2018, 10, 12, 20, 15, 40)
time2 = datetime.datetime(2015, 2, 10, 15, 41, 30)
  
difference = time1 - time2 
print('Difference is : ', difference)
    
minutes = divmod(difference.total_seconds(), 60
print('Difference in minutes: ', minutes[0],
      'minutes', minutes[1], 'seconds')
    
  
minutes = divmod(difference.seconds, 60
print('Total difference in minutes: ', minutes[0], 'minutes',minutes[1], 'seconds')


Output:

Difference is :  1340 days, 4:34:10
Difference in minutes:  1929874.0 minutes 10.0 seconds
Total difference in minutes:  274 minutes 10 seconds

Finding the execution time of a program

Now we will discuss how we can find the total time taken by the program to be executed.

Python3




from datetime import datetime
  
start_time = datetime.now()
  
for i in range(10000):
    i**100
  
end_time = datetime.now()
  
time_difference = (end_time - start_time).total_seconds() * 10**3
print("Execution time of program is: ", time_difference, "ms")


Output:

Execution time of program is:  24.488 ms

RELATED ARTICLES

Most Popular

Recent Comments