subtract two times in python subtract two times in python python python

subtract two times in python


Try this:

from datetime import datetime, datedatetime.combine(date.today(), exit) - datetime.combine(date.today(), enter)

combine builds a datetime, that can be subtracted.


Use:

from datetime import datetime, dateduration = datetime.combine(date.min, end) - datetime.combine(date.min, beginning)

Using date.min is a bit more concise and works even at midnight.

This might not be the case with date.today() that might return unexpected results if the first call happens at 23:59:59 and the next one at 00:00:00.


instead of using time try timedelta:

from datetime import timedeltat1 = timedelta(hours=7, minutes=36)t2 = timedelta(hours=11, minutes=32)t3 = timedelta(hours=13, minutes=7)t4 = timedelta(hours=21, minutes=0)arrival = t2 - t1lunch = (t3 - t2 - timedelta(hours=1))departure = t4 - t3print(arrival, lunch, departure)