-1

How can I get total hours in Odoo 11?

start_time = fields.Datetime("Start Time")
end_time = fields.Datetime("End Time")
total_hours = fields.Integer("Total Hours")

Thanks.

Roshin Raphel
  • 2,612
  • 4
  • 22
  • 40
Daniel Abaka
  • 21
  • 1
  • 5

1 Answers1

1

I achieved this using

@api.depends('start_time','end_time')
def get_hours(self):
    seconds = (self.end_time - self.start_time).total_seconds()
    self.total_hours = seconds // 3600

start_time = fields.Datetime("Start Time")
end_time = fields.Datetime("End Time")
total_hours = fields.Integer("Total Hours",compute="get_hours",store=True)

and if want minutes also you can try this:

@api.depends('start_time','end_time')
def get_hours(self):
    seconds = (self.end_time - self.start_time).total_seconds()
    hours = seconds // 3600
    minutes = (seconds % 3600) // 60
    self.total_hours = '{} hours, {} minutes'.format(hours, minutes)

start_time = fields.Datetime("Start Time")
end_time = fields.Datetime("End Time")
total_hours = fields.Char("Total Hours",compute="get_hours",store=True)
Pruthvi Barot
  • 1,948
  • 3
  • 14