简体   繁体   English

如何在 python 中使用 365 天格式计算两个日期/时间列表之间的时差?

[英]How can I calculate the time difference between two lists of dates/times using 365 day format in python?

I have two lists of dates and times in the following format: YYYY:DDD:HH:MM:SS:mmm (mmm is miliseconds).我有两个日期和时间列表,格式如下:YYYY:DDD:HH:MM:SS:mmm(mmm 是毫秒)。

I am getting these times from another list using regular expressions.我使用正则表达式从另一个列表中获取这些时间。

for line in start_list 
    start_time = re.search(r'((\d\d\d\d):(\d*):(\d\d):(\d\d):(\d\d):(\d\d\d))')
for line in end_list 
    end_time = re.search(r'((\d\d\d\d):(\d*):(\d\d):(\d\d):(\d\d):(\d\d\d))')

I could do a for loop cycling through start_time, and have a counter to keep track of the current line for end_time.我可以通过 start_time 循环循环,并有一个计数器来跟踪 end_time 的当前行。 I'm not really sure the best way to execute it.我不确定执行它的最佳方法。 I cant seem to figure out how to cycle through each line in each list to calculate the time difference between them.我似乎无法弄清楚如何循环遍历每个列表中的每一行来计算它们之间的时间差。 Any help would be very greatly appreciated.任何帮助将不胜感激。

You can convert to datetime objects and then just subtract the first series to the second as eg:您可以转换为日期时间对象,然后将第一个系列减去第二个系列,例如:

import pandas as pd

start_list_pts=[date.replace(":",".") for date in start_list]
end_list_pts=[date.replace(":",".") for date in end_list]

start_datetimes=pd.to_datetime(start_list_pts,format='%Y.%j.%H.%M.%S.%f')
end_datetimes=pd.to_datetime(end_list_pts,format='%Y.%j.%H.%M.%S.%f')

dts=start_datetimes-end_datetimes

Your milliseconds were not a fraction of your seconds that's why I changed the separator to points.您的毫秒数不是您秒数的一小部分,这就是我将分隔符更改为点的原因。 You can of course also only change the last one!您当然也可以只更改最后一个!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何计算 python 中两个日期和时间之间的差异 - how can i calculate the difference between two dates and times in python 如何在分钟内计算两次之间的差异 - python)How can I calculate difference between two times in mininute 如何使用时间模块计算 Python 中两个日期之间的天数差异? - How to calculate the difference in days between two dates in Python using the time module? 如何计算两个日期格式 Y/M/D h:m:s.ns 之间的差异 - How can I calculate the difference between two dates format Y/M/D h:m:s.ns 使用 ISO 8601 计算两次之间的时间差 - Calculate time difference between two times using ISO 8601 如何在python中以周为单位计算两个日期之间的差异 - How to calculate difference between two dates in weeks in python 计算 python 中两个日期之间的差异 - Calculate difference between two dates in python 如何使用Python查找两个日期之间的年份差异? (如果一个人超过18岁,请进行锻炼) - How can I find the difference in years between two dates using Python? (Work out if a person is over 18) 计算python中两次之间的差异 - Calculate the difference between two times in python 如何计算python中两次(含)之间的时间间隔(以秒为单位)? - How to calculate a time interval in seconds between two times (inclusive) in python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM