Odoo Help

1

Year only

By
louie
on 2/7/15, 4:19 AM 875 views

How to compute the year only? using fields.date i want to subtract the year in other field. could it be possible?

4

from dateutil.relativedelta import relativedelta
from datetime import datetime
from openerp import tools

end_date = datetime.strptime('10-02-2015', tools.DEFAULT_SERVER_DATE_FORMAT) # '10-02-2015' - Your date field
begin_date = datetime.strptime('10-02-2010', tools.DEFAULT_SERVER_DATE_FORMAT) # '10-02-2010' - Your date field
difference = relativedelta(end_date, begin_date).years
print(difference) # 5

1
louie
On 2/8/15, 7:55 PM

from dateutil.relativedelta import relativedelta
from datetime import datetime
from openerp import tools

end_date = datetime.strptime('10-02-2015', tools.DEFAULT_SERVER_DATE_FORMAT) # '10-02-2015' - Your date field
begin_date = datetime.strptime('10-02-2010', tools.DEFAULT_SERVER_DATE_FORMAT) # '10-02-2010' - Your date field
difference = relativedelta(end_date, begin_date).years
print(difference) # 5

==>regarding with your answer i try this and its error. when i add it in view.xml. what should i do?

   

This is the sample calculation code. Put it in a function and store the result in the field you want. You can call the function via onchange or make the field as functional field.

Dhinesh - Technical Consultant, Sodexis Inc
on 2/8/15, 10:07 PM
1
Zbik
On 2/7/15, 5:47 AM

Try this:

from openerp import tools
import datetime

dt = datetime.datetime.strptime(self.my_field_date, tools.DEFAULT_SERVER_DATE_FORMAT)
self.year = dt.strftime('%Y')  # long year
self.year = dt.strftime('%y') # short year

 

About This Community

This platform 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.

Register

Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

1 follower(s)

Stats

Asked: 2/7/15, 4:19 AM
Seen: 875 times
Last updated: 3/27/15, 8:10 PM