Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python subtract two dates in minutes

Tags:

python

date1 = datetime.datetime(2012, 10, 10, 10, 15, 44)
date2 = datetime.datetime(2012, 10, 17, 8, 45, 38)
roundedA = date2.replace(second = 0, microsecond = 0)
print roundedA
roundedB = date1.replace(second = 0, microsecond = 0)
print roundedB
minutes = (roundedA - roundedB).min
print minutes

Result is:

-999999999 days, 0:00:00

I want to count 2 different dates differences. I subtracted above, but it does not give me what I want. How to do subtract two dates and get the result in minutes or hours.

like image 317
John Smith Avatar asked Mar 17 '26 01:03

John Smith


1 Answers

The timedelta.min attribute does not represent minutes- it means the smallest possible timedelta (see here). If you want to get it in minutes, you can do:

d = roundedA - roundedB
minutes = d.days * 1440 + d.seconds / 60

This is because only seconds and days are stored internally in timedelta.

like image 190
David Robinson Avatar answered Mar 18 '26 13:03

David Robinson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!