Difference between two dates in Python
When working with dates, you may need to determine the difference between them. Python uses the strptime
method of the datetime
module for this purpose.
Let's find out how much time has passed between two dates: '25/05/2020 02:35:5'
and '15/06/2020 10:17:23'
. To do this, first apply the datetime
method. Then use the strptime
method, in the first parameter of which we pass the desired date. And in the second parameter we specify its format. Next, we display the resulting difference on the screen:
start_time = datetime.datetime.strptime('25/05/2020 02:35:5', '%d/%m/%Y %H:%M:%S')
end_time = datetime.datetime.strptime('15/06/2020 10:17:23', '%d/%m/%Y %H:%M:%S')
res = end_time - start_time
print(res) # 21 days, 7:42:18
Two dates are given:
dt1 = '13/10/2018 22:15:45'
dt2 = '15/11/2018 09:47:16'
Determine how much time passed between them.
Two dates are given:
dt1 = '01-12-2025 16:07:5'
dt2 = '31:12:2025 10:32:45'
Determine how much time passed between them.