[英]Display/return last 12 and 24 months with year from current month and year using python
[英]How to print remaining months from current month of a year in python pandas?
创建此 for 循环以通过传递范围来打印前几个月,但我想显示当前月份的未来月份,例如 month = 6 然后它应该打印一年中剩余的月份
year = 2021
month = 6
for i in range(1,month):
thdate = datetime(year,i,calendar.monthrange(year, i)[1])
thdate
IIUC,你可以试试
year = 2021
month = 6
for i in range(month, 13): # <--- Modify the `range` to [month, 13)
thdate = datetime(year,i,calendar.monthrange(year, i)[1])
另一个不同的解决方案是使用monthdelta包。
类似于 do-while 循环,或者您可以通过其他方式执行此操作(使用常规 for 循环):
from datetime import date
import monthdelta as md
curr_date = date(2021, 6, 1)
y = curr_date.year
first_iter = True
while first_iter or y == curr_date.year:
print(curr_date.month)
curr_date = curr_date + md.monthdelta(1)
first_iter = False
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.