简体   繁体   English

如何在 python 中使用 for 循环迭代日期

[英]how to iterate over date using for loop in python

I am creating an app where I have two values, a committee starting date for eg 2022,01,02 and for how many months it will continue here it is (4months).我正在创建一个应用程序,其中有两个值,一个委员会的开始日期,例如 2022,01,02,以及它将在这里持续多少个月(4 个月)。 Now I am saving some data in my database month wise and also these dates will save too.现在,我每月将一些数据保存在我的数据库中,这些日期也将保存。 now the issue is I am getting right result if the number of month is less than or equal to 12 using this.现在的问题是,如果月数小于或等于 12,我会得到正确的结果。

number of memebrs = 12
starting date = 2022,01,01
for i in range(1,17):
      print('date', (2022,i,10))

but the issue comes when the months are greater than 12 so than date start printing 2022,01,13 which is false because I also want to increment the year to 2023, I feel like this is not really a good idea very inefficient looking way.但是当月份大于 12 时问题就出现了,所以日期开始打印 2022,01,13 这是错误的,因为我还想将年份增加到 2023,我觉得这不是一个非常低效的好主意。 can anyone tell me is there any other way to do this.谁能告诉我有没有其他方法可以做到这一点。

you could do something like this:你可以这样做:

date = [2022,1,10]
for i in range(1,17):
    if i%12==0:
        date[0]+=1
        date[1]=1
    print('date', (time[0],i,time[2]))

While you can use the datetime library to handle dates, it doesn't provide any methods to increase dates month by month.虽然您可以使用datetime库来处理日期,但它不提供任何方法来逐月增加日期。

Now, previous suggestions/answers suggest you increase the year when month == 12, but that will cause December to be skipped.现在,以前的建议/答案建议您在月份 == 12 时增加年份,但这会导致十二月被跳过。 Also, your code doesn't consider any given month in the starting date.此外,您的代码不考虑开始日期中的任何给定月份。 So a better solution would be:所以更好的解决方案是:

>>> year = 2022
>>> month = 7
>>> day = 23
>>>
>>> for i in range(1, 8):
...     month += 1
...     if month == 13:
...         month = 1
...         year += 1
...     print(f'{year}-{month}-{day}')
...
2022-8-23
2022-9-23
2022-10-23
2022-11-23
2022-12-23
2023-1-23
2023-2-23

By the tone of your question i think you are a beginner, so i won't recommend you to use datetime module and i appreciate that you tried to do it on your own.从你的问题的语气来看,我认为你是一个初学者,所以我不建议你使用 datetime 模块,我很感激你尝试自己做。 What i dont appreciate is that why cant you just use if statements and create variables for year and date我不明白的是,为什么你不能只使用 if 语句并为年份和日期创建变量

yr = 2022
dt = 1
for i in range(1,17):
  if i % 12 == 0:
    yr += 1
    mn = 1
  print('date', (yr,i,dt))

I also want to share the modern aproach using datetime module.我还想分享使用 datetime 模块的现代方法。 But it requires some modules.但它需要一些模块。

In your cmd enter the command pip install python-dateutil在您的 cmd 中输入命令pip 安装 python-dateutil

Once installed close cmd and refresh your ide安装后关闭 cmd 并刷新您的 ide

this is the code you may want to use这是您可能要使用的代码

from datetime import datetime
from dateutil.relativedelta import relativedelta

date_time = datetime(2022, 1, 1) #Creating a Date object

for i in range(1, 17):
    date = date_time.date()
    print(date)
    date_time = date_time + relativedelta(months=1)

Please comment and tell me if you understood the modern aproach请评论并告诉我您是否了解现代方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM