给定开始和停止时间的间隔时间列表

时间:2016-11-17 16:58:50

标签: python list time

给定字符串开始和停止日期/时间以及我想要计算间隔时间的间隔数:

import datetime
from datetime import timedelta    
Start = '16 Sep 2016 00:00:00' 
Stop= '16 Sep 2016 06:00:00.00'
ScenLength = 21600 # in seconds (21600 for 6 hours; 18000 for 5 hours; 14400 for 4 hours)
stepsize = 10 # seconds
Intervals = ScenLength/stepsize

如何创建这些日期和时间的列表?

我是Python新手,到目前为止还不多:

TimeList=[]    
TimeSpan = [datetime.datetime.strptime(Stop,'%d %b %Y %H:%M:%S')-datetime.datetime.strptime(Start,'%d %b %Y %H:%M:%S')]     
    for m in range(0, Intervals):
        ...
        TimeList.append(...)

谢谢!

1 个答案:

答案 0 :(得分:1)

如果我理解正确,您希望定期找到时间戳。

可以使用Python类datetime.timedelta完成:

import datetime

start = datetime.datetime.strptime('16 Sep 2016 00:00:00', '%d %b %Y %H:%M:%S')
stop = datetime.datetime.strptime('16 Sep 2016 06:00:00', '%d %b %Y %H:%M:%S')

stepsize = 10
delta = datetime.timedelta(seconds=stepsize)

times = []
while start < stop:
    times.append(start)
    start += delta

print( times )

编辑:完整示例