简体   繁体   English

如何在 Python 中找到两个日期时间对象之间的毫秒差异?

[英]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.

相关问题 如何在python中找到两个日期时间对象之间的时差? - How do I find the time difference between two datetime objects in python? 如何计算 python 中 2 个日期时间之间的差异 - How do I calculate difference between 2 datetime in python 你如何在Python中获得两个时间对象之间的差异 - How do you get the difference between two time objects in Python 在python中以分钟为单位获取两个日期时间对象之间的差异 - Get the difference between two datetime objects in minutes in python 如何在 Python 中以毫秒为单位创建日期时间? - How do I create a datetime in Python from milliseconds? 如何使用Google日历api python找到两个事件之间的时差 - How do I find the time difference between two events using the Google calender api python dt.datetime 减去两个日期得到之间的时间我如何摆脱或美化秒和毫秒 - dt.datetime subtracting two dates to get the time between how do I get rid of or beautify the second and milliseconds 如何找到两个时区之间的小时差? - How do I find the difference in hours between two time zones? 我如何找到 python 中两个文件之间遇到的第一个区别? - How I find the first difference encountered between two Files in python? 如何在Python中找到不同时区的时间差异? - How do I find difference between times in different timezones in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM