【发布时间】:2019-09-17 02:08:48
【问题描述】:
我正在尝试创建一个数组,该数组将容纳从当前月份到指定的未来日期的每个月的日期(对于这种情况,我将使用 2022 年 12 月 31 日)。
到目前为止,我可以将其设置为获取给定年份中每个月的每一天,但仅从一月份开始。
我需要做的是,例如从 9 月开始,为 2022 年 9 月、10 月、11 月...12 月创建包含一个月中天数的数组。
到目前为止我的代码:
total = 0
days = []
dayst = []
for i in range(1,13):
month = datetime.datetime.strptime('{}'.format(i), "%m").strftime("%B")
length_of_month = calendar.monthrange(MODELYEAR, i)[1]
total = total + length_of_month
days.append(length_of_month)
dayst.append(total)
totaldays = sum(days)
有电流输出:
[31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
我基本上只需要动态结束时间并将开始时间转移到当前月份。
谢谢大家!
【问题讨论】:
标签: python