gpt4 book ai didi

Python的timedelta不适用于数据库记录不支持的timedelta秒组件类型: datetime.日期时间

转载 作者:太空宇宙 更新时间:2023-11-03 14:05:07 28 4
gpt4 key购买 nike

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

TypeError: unsupported type for timedelta seconds component: 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

对象。

关于Python的timedelta不适用于数据库记录不支持的timedelta秒组件类型: datetime.日期时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48958534/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com