[英]How do I find the difference in milliseconds between two datetime objects in Python?
I have two datetime objects which are 02:49:01.210000 and 02:49:01.230000:我有两个日期时间对象,它们是 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()
How do I workout the difference between these two times in milliseconds?我如何以毫秒为单位计算这两次之间的差异?
I tried doing RA_1 - RA_2 but got the error:我尝试执行 RA_1 - RA_2 但出现错误:
unsupported operand type(s) for -: 'datetime.time' and 'datetime.time'
I also tried using total_seconds() but got the error:我也尝试使用 total_seconds() 但得到了错误:
'datetime.time' object has no attribute 'total_seconds'
This is how you can compute the different between two time
objects.这就是您如何计算两个
time
对象之间的差异。 It is a hack which involves adding the same date to both objects.这是一个 hack,涉及向两个对象添加相同的日期。
By construction it assumes both times relate to the same day.通过构建,它假设两个时间都与同一天有关。
from datetime import datetime, date, time
obs_data = {'RA': "22:24:05.52" }
pred_data = {'RA':"22:24:05.60"}
RA_1 = datetime.strptime(obs_data['RA'], '%H:%M:%S.%f').time()
RA_2 = datetime.strptime(pred_data['RA'], '%H:%M:%S.%f').time()
diff = datetime.combine(date.today(), RA_2) - datetime.combine(date.today(), RA_1)
diff.total_seconds() * (10 ** 3)
# 80.0 [ms]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.