####
account.txt文档内容示例:
2020-11
2021-05
import datetime
with open(r'account.txt', 'r', encoding='utf-8') as file:
lines = file.readlines()
beginMonth = lines[0].strip('\n')
endMonth=lines[1].strip('\n')
def monthRange(beginMonth, endMonth):
months = set([])
mt = datetime.datetime.strptime(beginMonth, "%Y-%m")
month = beginMonth[:]
while month <= endMonth:
months.add(month)
mt = mt + datetime.timedelta(1)
month = mt.strftime("%Y-%m")
monthList = sorted(months)
return monthList
runMonth_list = monthRange(beginMonth, endMonth)
print(runMonth_list)
print(len(runMonth_list))
for i in range(1,len(runMonth_list)+1):
print(runMonth_list[-i])
python实战案例之日期加减(月份加减)
最新推荐文章于 2024-05-14 10:33:24 发布