Sumit Dwivedi
Sumit Dwivedi

Reputation: 59

Getting unsupported operand type error while trying to subtract two date values

I am not why I am getting error in below code -

from datetime import datetime
import time
start =datetime.now().replace(microsecond=0).isoformat(' ')
end = datetime.now().replace(microsecond=0).isoformat(' ')
print(f" Total Time taken by check run is {end - start}[hh:mm:ss]")

Error is coming in the print statement saying:

TypeError: unsupported operand type(s) for -: 'str' and 'str'

Upvotes: 1

Views: 60

Answers (1)

PacketLoss
PacketLoss

Reputation: 5746

isoformat() returns a string.

If you remove isoformat() from your datetime object, you will be able to subtract the time difference.

start = datetime.now().replace(microsecond=0)
end = datetime(2021, 4, 9, 17, 15, 00)
print(f" Total Time taken by check run is {end - start}[hh:mm:ss]")

#Total Time taken by check run is 1:26:18[hh:mm:ss]

Upvotes: 2

Related Questions