How to receive a list of each date between two dates in python

Asked

Viewed 64 times

-3

At the point I’m at now, I need to get a function that returns me a list of all the dates between two dates.

I don’t know exactly how to do this operation, if I can use some specific library as datetime and how I would use exactly to get this result.

I am using dates in dd/mm/yyyy format

  • 1

    Mariano, edit your question and put example of your project, and the code of what you are trying to run.

3 answers

0


Using the library datetime

from datetime import date, timedelta

data_inicio = date(2021, 1, 1)
data_fim = date(2021, 2, 3)

delta = data_fim - data_inicio

lista_datas = []
for i in range(delta.days + 1):
    day = data_inicio + timedelta(days=i)
    lista_datas.append(day)


print(lista_datas)

The result will be

[datetime.date(2021, 1, 1), datetime.date(2021, 1, 2), datetime.date(2021, 1, 3), datetime.date(2021, 1, 4), datetime.date(2021, 1, 5), datetime.date(2021, 1, 6), datetime.date(2021, 1, 7), datetime.date(2021, 1, 8), datetime.date(2021, 1, 9), datetime.date(2021, 1, 10), datetime.date(2021, 1, 11), datetime.date(2021, 1, 12), datetime.date(2021, 1, 13), datetime.date(2021, 1, 14), datetime.date(2021, 1, 15), datetime.date(2021, 1, 16), datetime.date(2021, 1, 17), datetime.date(2021, 1, 18), datetime.date(2021, 1, 19), datetime.date(2021, 1, 20), datetime.date(2021, 1, 21), datetime.date(2021, 1, 22), datetime.date(2021, 1, 23), datetime.date(2021, 1, 24), datetime.date(2021, 1, 25), datetime.date(2021, 1, 26), datetime.date(2021, 1, 27), datetime.date(2021, 1, 28), datetime.date(2021, 1, 29), datetime.date(2021, 1, 30), datetime.date(2021, 1, 31), datetime.date(2021, 2, 1), datetime.date(2021, 2, 2), datetime.date(2021, 2, 3)]

Update - How to get dates in string format

from datetime import datetime

lista_datas_str = [datetime.strftime(dt, format="%d/%m/%Y") for dt in lista_datas]

print(lista_datas_str)
['01/01/2021', '02/01/2021', '03/01/2021', '04/01/2021', '05/01/2021', '06/01/2021', '07/01/2021', '08/01/2021', '09/01/2021', '10/01/2021', '11/01/2021', '12/01/2021', '13/01/2021', '14/01/2021', '15/01/2021', '16/01/2021', '17/01/2021', '18/01/2021', '19/01/2021', '20/01/2021', '21/01/2021', '22/01/2021', '23/01/2021', '24/01/2021', '25/01/2021', '26/01/2021', '27/01/2021', '28/01/2021', '29/01/2021', '30/01/2021', '31/01/2021', '01/02/2021', '02/02/2021', '03/02/2021']

end of update

I hope it helps

  • Thank you for your attention Paulo Marques. How can I turn this reply into a string with this schema: dd/mm/yyyy ?

  • @Marianotupa, I updated the post.

0

import datetime                                                                                                             
data = datetime.datetime(2014,7,16,23)
dataatual = datetime.datetime.now()                           
diff = dataatual - data
days = diff.days
anos, days = days // 365, days % 365                          
meses, days = days // 30, days % 30
seconds = diff.seconds
horas, seconds = seconds// 3600, seconds % 3600              
minutos, seconds = seconds // 60, seconds % 60                                                                              
print("Desde {} passaram {} anos, {} meses, {} dias, {} horas, {} minutos e {} segundos".format(data, anos, meses, days, horas, minutos, seconds))

You need to use a library to pick up the current time, you can make some modifications to this code also so the user can type the date.

0

The way I managed to solve the problem was like this, maybe it’s not the best solution, but it worked for my purpose.

def date_generator(self, data, data_final):

    data_dd = data[:2]
    data_dd = int(data_dd)

    data_mm = data[3:]
    data_mm = data_mm[:2]
    data_mm = int(data_mm)

    data_yy = data[6:]
    data_yy = int(data_yy)

    data_final_dd = data_final[:2]
    data_final_dd = int(data_final_dd)

    data_final_mm = data_final[3:]
    data_final_mm = data_final_mm[:2]
    data_final_mm = int(data_final_mm)

    data_final_yy = data_final[6:]
    data_final_yy = int(data_final_yy)


    data_inicio = date(data_yy, data_mm, data_dd)

    data_fim = date(data_final_yy, data_final_mm, data_final_dd)

    delta = data_fim - data_inicio

    lista_datas = []

    for i in range(delta.days + 1):
        day = data_inicio + timedelta(days=i)
        lista_datas.append(day.strftime('%d/%m/%y'))

    return lista_datas
  • There is a much easier way to turn strings into dates, with strptime. And instead of creating a timedelta every iteration of for, another alternative is to increase the initial date by 1 day until it is greater than the final date: https://ideone.com/JORLGI

Browser other questions tagged

You are not signed in. Login or sign up in order to post.