如何找到Python中两个datetime对象之间的毫秒差异?

如何找到Python中两个datetime对象之间的毫秒差异?,python,python-2.7,datetime,Python,Python 2.7,Datetime,我有两个datetime对象,分别是02:49:01.210000和02:49:01.230000: RA_1 = datetime.datetime.strptime(obs_data['RA'][3], "%H:%M:%S.%f").time() RA_2 = datetime.datetime.strptime(pred_data['RA'][3], "%H:%M:%S.%f").time() 如何以毫秒计算这两次之间的差异 我尝试执行RA_1-RA_2,但出现错误: unsupport

我有两个datetime对象,分别是02:49:01.210000和02:49:01.230000:

RA_1 = datetime.datetime.strptime(obs_data['RA'][3], "%H:%M:%S.%f").time()
RA_2 = datetime.datetime.strptime(pred_data['RA'][3], "%H:%M:%S.%f").time()
如何以毫秒计算这两次之间的差异

我尝试执行RA_1-RA_2,但出现错误:

unsupported operand type(s) for -: 'datetime.time' and 'datetime.time'
'datetime.time' object has no attribute 'total_seconds'
我还尝试使用total_seconds(),但出现错误:

unsupported operand type(s) for -: 'datetime.time' and 'datetime.time'
'datetime.time' object has no attribute 'total_seconds'

这就是计算两个
时间
对象之间差异的方法。这是一种将相同日期添加到两个对象的黑客行为

通过构造,它假定两个时间都与同一天有关


您没有两个datetime对象;您对它们调用了
time
,因此您得到了时间对象。你为什么这么做?可以help@user2357112因为我不想把日期考虑在内,所以我只想要时间。@MattHill:
datetime.time
不支持这一点,因为时差是不明确的。上午1点是晚上11点后2小时,还是22小时前,还是26小时前?如果你想要一些特定的行为,你需要手动解决歧义。那太好了!非常感谢你!没问题,我添加了一个免责声明,它假设两个时间都是同一天,因此晚上11点和凌晨1点的时间差是22小时,而不是24小时。如果它有效,请随意接受(在左边打勾)。同样重要的是要注意,这不会处理DST和其他奇怪的问题。但是如果没有日期和时区信息,你真的无法做到这一点,我们这里没有。