lundi 14 septembre 2020

How to calculate the difference between 2 dates, split them dynamically? Python

I have to write a function which will count a difference in days between 2 dates. If the number of days exceeds 30, then split it in n date ranges and save in list or dict. I have started with the function but cannot finish it. The function has to calculate dynamic values.

For example

start_date = '2020-07-01'
end_date = '2020-09-15'


difference = (end_date - start_date).days


dateranges = []
dateranges.append(start_date)


if difference > 30:
    end_date = start_date + dt.timedelta(days=30)
    dateranges.append(end_date)

But I do not get how to make it cyclic, when it take each time a new start_date and end_date and calculates the difference between them. For example I always add 30 dys here but it can be that a less number of days has to be added.

Aucun commentaire:

Enregistrer un commentaire