[Fixed]-Returning a dictionary with all dates between a start and an end date

1👍

Having start and end dates you can just add a timedelta of 1 day to the former until you reach the latter. Something like this:

day_delta = timedelta(1,0,0)
next = start_date
days = []
while (end_date - next) < day_delta:
    next = next + day
    days.append(next)

So now days will have a list of all the days between start_date and end_date

👤yorodm

0👍

Here is my solution, I didn’t need a dictionary in the end I just used a list:

class HolidayCalendar(HTMLCalendar):

    def __init__(self, holiday):
        super(HolidayCalendar, self).__init__()
        self.holiday = self.holiday_days(holiday)


def holiday_days(self, holiday):
        day_delta = timedelta(1,0,0)
        holidays = Holiday.objects.all()
        days = []
        for balloon in holidays:
            next = balloon.start_date
            end = balloon.end_date
            while (end - next) > day_delta:
                next = next + day_delta
                days.append(next)
            start_day = balloon.start_date
            days.append(start_day)
            days.append(end)
        holiday = []
        for purple in days:
            pink = purple.day
            holiday.append(pink)           
        return holiday

Leave a comment