2016-11-17 45 views
1

给定的字符串开始和结束日期/时间和间隔时间的编号,而我想之间以统计时间:创建的给定发车间隔时间的列表和停止时间

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(...) 

谢谢!

回答

0

如果我理解正确,您想要定期查找时间戳记。

可以与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) 

编辑:完整的例子

+0

谢谢!这工作完美。 – LHB

相关问题