[Answer]-Django datetime expression


Django represents DateTimeFields as Python datetime objects, so the result of your subtraction is a datetime.timedelta object. To get the number of full days, you can do:

delta = self.end_date - self.pub_date
return delta.days

This only counts full days, though. To round to the nearest full day, you can do something like:

delta = self.end_date - self.pub_date
if delta.seconds / 3600 >= 12:
    return delta.days + 1 # round up
    return delta.days # round down

For more information, see: http://docs.python.org/2/library/datetime.html#timedelta-objects

Leave a comment