[PYTHON] TypeError: unsupported operand type (s) for-: What to do when'datetime.time'and'datetime.time'

--Environment --macOS Catalina version 10.15.7 - Python 3.8.5 - pandas 1.1.3

Event: TypeError: unsupported operand type (s) for-:'datetime.time' and'datetime.time'

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)

Cause: datetime.time and datetime.date cannot be calculated as is

-[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.

Correspondence: Convert to datetime.datetime and calculate

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

TypeError: unsupported operand type (s) for-: What to do when'datetime.time'and'datetime.time'
TypeError: unsupported operand type (s) for /:'list' and'float'
What to do when raise ValueError, "unsupported hash type"
[For beginners] What to do after installing Anaconda
What to do if rails s doesn't work
What to do when "TypeError: data type not understood" appears in python's numpy.zeros
What to do if Python doesn't work on Git for Windows
What to do when the value type is ambiguous in Python?
What to do when Ubuntu crashes
What to do if yum breaks
What to do with Magics install
What to do with PYTHON release?
What to do to get tensorflow-gpu to work
What to do if PyInstaller3.5 gives an error in Python3.8 (TypeError: an integer is required (got type bytes))
What to do if No Python documentation found for ... appears in pydoc
What to do if you get a TypeError with numpy min, max
After all, what should I use to do type comparisons in Python?
What to do after installing Linux (Ubuntu)
Let's summarize what you want to do.
What to do when "TypeError: must be string, not int…" appears when using strptime
What to do when xlrd.biffh.XLRDError: Unsupported format, or corrupt file: Expected BOF record;