Skip to Content
Menu
This question has been flagged
4 Replies
8627 Views

Hi,

I have created some records in 'test.test' table, i have two fields

created_date = field.Date(string='Created date')

days_difference = fields.Integer(string='Aging' store=True, readonly=True)

i need to calculate the days difference (today-created_date) in a every day for each record in the list view


Please help me to calculate this

Avatar
Discard
is this work every day


On Sat, Oct 5, 2019 at 12:57 PM Cybrosys Techno Solutions Pvt.Ltd <odoo@cybrosys.com> wrote:

A new answer on How to calculate the Days difference (created date - today) has been posted. Click here to access the post :

See post


Sent by Odoo S.A. using Odoo.

Best Answer

Hi Prajith try following code,

import datetime

created_date = fields.Date(string='Created date')

@api.model
def _compute_difference(self):
for rec in self:
converted_date = datetime.datetime.strptime(rec.created_date, '%Y-%m-%d').date()
rec.days_difference = (datetime.date.today() - converted_date).days

days_difference = fields.Integer(string='Aging', store=True, compute=_compute_difference)
Thanks.
Avatar
Discard
Best Answer
days_difference = fields.Integer(compute='_compute_difference')
def _compute_difference(self):
    for rec in self:
        rec.days_difference = (date time.today()- rec.created_date).days            ​                ​              ​                ​

Hope this will help you


Avatar
Discard

Can you please elaborate more on this?, but for field computation not backend coding.

This working for me:
* rec.days_difference = (datetime.today().date()- rec.created_date).days

Best Answer

You can use this date difference.

[('date','<=', ((context_today()-datetime.timedelta(days=10)).strftime('%Y-%m-%d')))]


Avatar
Discard
Best Answer
created_day = fields.Date(default=fields.Date.context_today)
days_difference = fields.Integer(compute="_compute_difference", readonly=True)
@api.depends("created_day")
def _compute_difference(self):
for rec in self:
today = fields.Date.from_string(fields.Date.today())
crt_day = fields.Date.from_string(rec.created_day)

difference = (crt_day - today).days
rec.days_difference = difference
Avatar
Discard