日期循环(day循环)

import datetime
def day_range(bgn,end):
    fmt = '%Y%m%d'
    begin=datetime.datetime.strptime(bgn,fmt)
    end=datetime.datetime.strptime(end,fmt)
    delta=datetime.timedelta(days=1)

    interval=int((end-begin).days) +1
   
#     return [begin+delta*i for i in range(0,interval,1)]
    for i in range(0,interval,1):
        day1 = begin+delta*i
        day2 = begin+delta*(i+1)
        strday = day1.strftime('%Y%m%d%H%M%S')
        strhour1=day1.strftime('%Y%m%d'+'09'+'%M%S')
        strhour2=day2.strftime('%Y%m%d'+'08'+'%M%S')
        print(strday,strhour1,strhour2)

#     return strday

day_range('20160101','20160201')

 

posted @ 2022-09-27 20:26  EROEG  阅读(83)  评论(0编辑  收藏  举报