--Environment --macOS Catalina version 10.15.7 - Python 3.8.5 - pandas 1.1.3
I got angry when I subtracted the time
Traceback (most recent call last):
File "/Users/mananakai/tryPython/main.py", line 30, in calc_diff
diff = end - start
TypeError: unsupported operand type(s) for -: 'datetime.time' and 'datetime.time'
def calc_diff(start, end):
diff = end - start
print(diff)
-[python] datetime.time type to add and subtract time
It cannot be calculated only by time information (datetime.time) and date information (datetime.date). Date and time information (datetime.datetime) is used for the calculation.
The calculation result is the difference information between times (datetime.timedelta).
def calc_diff(start, end):
today = datetime.date.today()
d_start = datetime.datetime.combine(today, start)
d_end = datetime.datetime.combine(today, end)
diff = d_end - d_start
print(type(diff)) # >> <class 'datetime.timedelta'>
Recommended Posts