Python list of first day of month for given period

后端 未结 8 1601
有刺的猬
有刺的猬 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 03:03

    This is the same as @Andrew's answer, but much faster:

    dates = []
    date = datetime.date.today()
    while date.year < 2015:
        if date.day == 1:
            dates.append(date)
            # I added the following line:
            date += datetime.timedelta(days=27)
        date += datetime.timedelta(days=1)
    

    Using force, but not being brutal.

提交回复
热议问题