【问题标题】:Create list of interval times given start and stop time创建给定开始和停止时间的间隔时间列表
【发布时间】:2016-11-17 16:58:50
【问题描述】:

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

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

谢谢!

【问题讨论】:

    标签: python list time


    【解决方案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 )
    

    编辑:完整示例

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多