[Answered ]-Getting number of days between twoo dates

1👍

You can subtract the two date objects, and then obtain the .days attribute, so:

class Reservation(models.Model):
    # …
    
    @property
    def days(self):
        return (self.end_date - self.start_date).days

This will thus return the difference in days, so that means that if we calculate the difference between today and yesterday, this will result in one day:

>>> (date(2021, 4, 28) - date(2021, 4, 27)).days
1

If you want to take both days into account, you increment the result with 1:

class Reservation(models.Model):
    # …
    
    @property
    def days(self):
        return (self.end_date - self.start_date).days + 1

You can thus retrieve the number of days from a Reservation object with:

my_reservation.days

Leave a comment