尝试减去两个日期值时出现不支持的操作数类型错误



我不是为什么我在下面的代码中出错-

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]")

打印声明中出现错误:

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

isoformat()返回一个string

如果从datetime object中删除isoformat(),则可以减去时间差。

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]

最新更新