繁体   English   中英

Python 的 timedelta 不适用于 timedelta seconds 组件不支持的数据库记录类型:datetime.datetime

[英]Python's timedelta doesn't work on database records unsupported type for timedelta seconds component: datetime.datetime

我需要比较数据库中的两个时间戳。 首先,我将一条记录另存为字典,然后将另一条记录保存为字典,然后比较这些字典中的日期时间组件并得到以下错误消息:

类型错误:timedelta 秒组件的类型不受支持:datetime.datetime

相关代码是:

import datetime
# got records from database, then fromated dictionary:
record_1 = {'timestamp': timestamp, 'x': x, 'y': y}
# latter on
record_2 = record_1
record_1 = {'timestamp': timestamp, 'x': x, 'y': y}
# Records prints:
# {'timestamp': datetime.datetime(2018, 2, 21, 6, 47, 38), 'x': 1, 'y': 2}
# {'timestamp': datetime.datetime(2018, 2, 16, 21, 59, 59), 'x': 3, 'y': 4}
# Respectively, error ocures in line:
difference = datetime.timedelta(record_1['timestamp'], record_2['timestamp'])

可能是什么问题呢?

要比较两个datetime.datime对象,您只需要做如下减法:

difference = record_1['timestamp'] - record_2['timestamp']

这将导致:

datetime.timedelta

目的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM