输入日期,获得该日期往后一个月同一天,连续获得多个月
作者:互联网
def getNextDates(year,month,day,times):
dateList = []
for i in range(times):
tmpYear = year + (month+i)//12
tmpMonth = (month+i) % 12+1
tmpDay = day
try:
dateList.append(datetime.date(tmpYear,tmpMonth,tmpDay))
except:
if tmpMonth == 12:
dateList.append(datetime.date(tmpYear+1, 1, tmpDay))
else:
dateList.append(datetime.date(tmpYear, tmpMonth + 1, 1))
return dateList
if __name__ == "__main__":
print(getNextDates(2008,3,30,12))
执行结果
[datetime.date(2008, 4, 30), datetime.date(2008, 5, 30), datetime.date(2008, 6, 30), datetime.date(2008, 7, 30), datetime.date(2008, 8, 30), datetime.date(2008, 9, 30), datetime.date(2008, 10, 30), datetime.date(2008, 11, 30), datetime.date(2008, 12, 30), datetime.date(2009, 1, 30), datetime.date(2009, 3, 1), datetime.date(2009, 3, 30)]
相对获取单个结果,主要区别在于循环获取并处理年份和月份上,年份需要+月份除以12的整数部分,月份为循环获得月份除以12获得余数上面
标签:12,同一天,30,dateList,datetime,获得,日期,date,2008 来源: https://blog.csdn.net/wwenww/article/details/122722025