Search code examples
pythonodoo-8odooodoo-9

Adding days to datetime


I wan't to add to planned date integer. It's always will be from 1 to 10, but having problem with my code.

planned_date = fields.Date(string='Planned Date', required=False,
                           default=fields.Date.today)
def linedate(self):    
    if line.discount > 5:
       
        daysz = line.product_id.seller_ids[0].delay # it's integer from 1 to 10 always
   
        planned = (line.planned_date + timedelta(days=daysz) ).strftime('%Y-%m-%d')
        line.planned_date =  planned
        print line.planned_date

I'm getting this kind of error

planned = (line.planned_date + timedelta(days=daysz) ).strftime('%Y-%m-%d')

TypeError: coercing to Unicode: need string or buffer, datetime.timedelta found


Solution

  • line.planned_date is a Unicode string object. You need to convert it to a datetime object and then add using timedelta.

    Ex:

    import datetime
    planned = (datetime.datetime.strptime(line.planned_date, '%Y-%m-%d') + datetime.timedelta(days=daysz) ).strftime('%Y-%m-%d')