跳转至

🔥AI副业赚钱星球

点击下面图片查看

郭震AI

133

编辑日期: 2024-11-28 文章阅读:

@author jackzhenguo
@desc 
@date 2020/1/1

133 获得某天后的1~n天

import calendar
from datetime import date,datetime

def getEverydaySince(year,month,day,n=10):
    i = 0
    _, days = calendar.monthrange(year, month)
    while i < n: 
        d = date(year,month,day)    
        if day == days:
            month,day = month+1,0
            _, days = calendar.monthrange(year, month)
            if month == 13:
                year,month = year+1,1
                _, days = calendar.monthrange(year, month)
        yield d
        day += 1
        i += 1

测试结果:

In [3]: for day in getEverydaySince(2020,2,1): 
   ...:     print(day)                                                                      
2020-02-01
2020-02-02
2020-02-03
2020-02-04
2020-02-05
2020-02-06
2020-02-07
2020-02-08
2020-02-09
2020-02-10

上一个例子 下一个例子

京ICP备20031037号-1