'odoo calculate age from a date field and today date
I want to get an age between a field and today date
from openerp import models, fields, api, _
from openerp import SUPERUSER_ID
from datetime import date
class report_purchase_stock(models.Model):
_name='report.purchase.stock'
etaw_update = fields.Date('ETA Warehouse') // 2022-01-24
etaw_age = fields.Char('Days in Warehouse')
I have tried something like this from the forum but it return empty
@api.onchange('etaw_update')
def set_age(self):
for rec in self:
if rec.etaw_update:
dt = rec.etaw_update
d1 = datetime.strptime(dt, "%Y-%m-%d").date()
d2 = date.today()
rd = relativedelta(d2, d1)
rec.etaw_age = str(rd.years) + ' years'
What I want is something like 1 month 14 days
Solution 1:[1]
I assume that etaw_update is a date where the product entering warehouse for the first time, and etaw_age is the total days where that product has been stored. Is that right?
If that so, you may want to define a current_date variable which will get the todays date, and then you can subtract that current_date with etaw_update
@api.onchange('etaw_update')
def set_age(self):
for rec in self:
current_date = date.today()
date_in_your_field = rec.etaw_update
rec.etaw_age = current_date - date_in_your_field
print(str(rec.etaw_age))
Please let me know if this what you need
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | altela |
