0

I am trying to calculate time between one date and another and give result in hours rounding up hours.

I tried just substracting, both dates are datetime total_hours = self.valid_until - self.started_at the result is timedelta. I tried this one but it seems like it is viable for datetime field only

         (total_hours.replace(second=0, microsecond=0, minute=0, hour=total_hours.hour)
                +timedelta(hours=total_hours.minute//30))
  • 3
    If it is a timedelta object, you can use total_seconds and divide by 3600. `total_hours = round((self.valid_until - self.started_at).total_seconds() / 3600)` – Sagun Shrestha Aug 08 '23 at 23:35
  • 1
    Not forgetting to [round up](https://stackoverflow.com/questions/2356501/how-do-you-round-up-a-number) the result with math.ceil or equivalent. – jarmod Aug 09 '23 at 00:09

0 Answers0