Python list of first day of month for given period

后端 未结 8 1588
有刺的猬
有刺的猬 2021-02-09 02:20

I am trying find an efficient way of creating a list of dates only including the first day of the month for a given period. Something like this but better:

impor         


        
8条回答
  •  别跟我提以往
    2021-02-09 02:58

    If you're only creating the list for a few years then efficiency should not be a concern. Clarity of code is the most important aspect.

    dates = []
    date = datetime.date.today()
    while date.year < 2015:
        if date.day == 1:
            dates.append(date)
        date += datetime.timedelta(days=1)
    

提交回复
热议问题