I have a scenario such like that. i have a start date and end date in odoo from. i want to find the days between two given dates and those days need to be compared with other model's week days.
            Asked
            
        
        
            Active
            
        
            Viewed 1,191 times
        
    0
            
            
        - 
                    Tanke a look to this question https://stackoverflow.com/questions/17833176/postgresql-days-months-years-between-two-dates – Frank N Stein Mar 05 '21 at 14:25
- 
                    You can take the example - https://github.com/odoo/odoo/blob/14.0/addons/crm/models/crm_lead.py#L275 – Dipen Shah Mar 09 '21 at 07:01
2 Answers
0
            
            
        You can use this function in python code for finding days between two dates.
from datetime import date
f_date = date(2014, 7, 2)
l_date = date(2014, 7, 11)
delta = l_date - f_date
print(delta.days)
 
    
    
        Bhoomi Vaishnani
        
- 718
- 4
- 19
0
            
            
        Bhoomi's answer gave you the easiest way to do it with pure python.
I just wanted to expand on that a bit and show you specifically working with Odoo fields. If you have 2 fields.Date or fields.Datetime fields on a model, it might be easiest to add another field to compute days between then.
from odoo import api, fields, models
class MyClass(models.Model):
    _name = "my.class"
    start = fields.Date()
    stop = fields.Date()
    days_until = fields.Integer(
        compute="_compute_days_until",
        store=True
    )
    @api.depends("start", "stop")
    def _compute_days_until(self):
        for record in self:
            if record.start and record.stop:
                record.days_until = (record.stop - self.record).days
            else:
                record.days_until = 0
Depending on your version of Odoo, the start and stop values in those examples may be string or actual datetime.date object. In older version of Odoo I believe they are strings.
But Odoo has a helper if you run into that:
from odoo import fields
fields.Date.from_string()
So your compute may potentially need to look more like:
record.days_until = (fields.Date.from_string(record.stop) - fields.Date.from_string(record.start)).days
 
    
    
        Holden Rehg
        
- 917
- 5
- 10
