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

2024-10-01 02:34:16 发布

您现在位置:Python中文网/ 问答频道 /正文

给定字符串开始和结束日期/时间以及我要计算其间时间的间隔数:

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

谢谢!你知道吗


Tags: inimportfordatetime时间startsepstop
1条回答
网友
1楼 · 发布于 2024-10-01 02:34:16

如果我没听错的话,你想找一个固定间隔的时间戳。你知道吗

这可以通过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 )

编辑:完整示例

相关问题 更多 >