[英]How do I find the time difference between two datetime objects in python?
[英]How do I find the difference in milliseconds between two datetime objects in Python?
我有兩個日期時間對象,它們是 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'
我也嘗試使用 total_seconds() 但得到了錯誤:
'datetime.time' object has no attribute 'total_seconds'
這就是您如何計算兩個time
對象之間的差異。 這是一個 hack,涉及向兩個對象添加相同的日期。
通過構建,它假設兩個時間都與同一天有關。
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.