Skip to content

Latest commit

 

History

History
48 lines (40 loc) · 960 Bytes

133.md

File metadata and controls

48 lines (40 loc) · 960 Bytes
@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
[上一个例子](132.md) [下一个例子](134.md)