How to calculate the duration between two datetimes in Python

2 Answers

0 votes
from datetime import datetime

start = datetime(2025, 6, 1, 12, 0, 0)
end   = datetime(2025, 6, 3, 15, 30, 0)

duration = end - start

print(duration)

print(duration.days) 
print(duration.seconds) # 3*3600 + 30*60 = 12600
print(duration.total_seconds())



'''
run:

# 3*3600 + 30*60 = 12600

'''

 



answered Jan 30 by avibootz
0 votes
from datetime import datetime

start = datetime.now()

for i in range(1, 10_000_000):
    pass  # or do something with i

end = datetime.now()

elapsed = end - start

print("Elapsed:", elapsed)



'''
run:

Elapsed: 0:00:00.506004

'''

 



answered Jan 30 by avibootz
...