from dateutil import relativedelta
In this method we use 'relativedelta', you can import relativedelta from dateutil.
in this case, there are 2 fields date 'date_from' and 'date_to'.
we make variable diff for
@api.depends('date_from','date_to')
def _compute_duration(self):
for i in self:
diff = relativedelta.relativedelta(i.date_from, i.date_to)
years = diff.years
if years > 0:
i.duration = str(years)+" Years "+str(diff.months)+" Months "+str(diff.days)+" Days"
else:
i.duration = str(diff.months) + " Months" + str(diff.days) + " Days"
.......
@api.depends('test_start','test_end')
def _compute_duration(self):
for i in self:
diff = relativedelta.relativedelta(i.test_end, i.test_start)
hours = diff.hours
if hours > 0:
i.duration=str(hours)+" Hours "+str(diff.minutes)+" Minutes "+str(diff.seconds)+" Seconds"
else:
i.duration = str(diff.minutes) + " Minutes " + str(diff.seconds) + " Seconds"