文章内容

2020/8/26 17:02:26,作 者: 黄兵

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

在python中做时间运算的时候,出现了如下错误:

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

出现问题的原因:

主要是字符串于datetime做运算,类型不同。

解决方案:

将字符串格式的时间转换为datetime类型的时间,代码如下:

last_time_format = datetime.strptime(last_time, '%Y-%m-%d %H:%M:%S.%f')

其中:last_time是字符串类型的时间。

如果出现格式不匹配,可以看看这篇文章:ValueError: time data '2020-08-26 03:33:13.449150' does not match format 'YYYY-MM-DD HH:MM:SS'


黄兵个人博客原创。

转载请注明出处:黄兵个人博客 - TypeError: unsupported operand type(s) for -: 'datetime.datetime' and 'str'

分享到:

发表评论

评论列表