Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

1

How to retrieve correct date format when calculating end date?

By
DM
on 2/19/15, 7:24 AM 694 views

Hello odoo community,

i have a field end date which is computed (start date + duration) compute  works but for some reason my results retrieve date format like 2015-12-19 00:00:00 any ideas how to get only 2015-12-19?

Any help or examples would be appreaciated.

imports:

from openerp.osv import osv, fields
from openerp import api
import time
from datetime import date
from datetime import datetime
from dateutil.relativedelta import relativedelta

 

Field :

'start_date': fields.date('Start Date', required=True),
'duration': fields.integer('Duration', required=True, help='Estimated project duration in months', store=True),

'end_date': 'fields.char(compute='_end_date', string='End Date', help='Calculated as start date plus duration in months'),

    _defaults = {
        'start_date': fields.date.context_today,
        }

compute function:

    @api.one
    @api.depends('start_date','duration')
    def _end_date(self):
        self.end_date = datetime.strptime(self.start_date, '%Y-%m-%d') + relativedelta(months=self.duration)

 

Thank you

It worked Thanks!!!

DM
on 2/19/15, 7:36 AM
1
Jordan Vrtanoski
On 2/19/15, 7:29 AM

Convert the date to string with explicit format like in the example below

 def _end_date(self):
        self.end_date = (datetime.strptime(self.start_date, '%Y-%m-%d') + relativedelta(months=self.duration)).strftime('%Y-%m-%d')

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 2/19/15, 7:24 AM
Seen: 694 times
Last updated: 10/23/15, 9:29 AM