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

Year only

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

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

4

Dhinesh - Technical Consultant, Sodexis Inc

--Dhinesh - Technical Consultant, Sodexis Inc--
3577
| 5 4 9
Pondicherry, India
--Dhinesh - Technical Consultant, Sodexis Inc--

Passionate coder in Python/OpenERP. Knows Django/Flask MVC frameworks. Did code in Java, PHP.

Contact me at: dvdhinesh.mail@gmail.com

Dhinesh - Technical Consultant, Sodexis Inc
On 2/7/15, 5:53 AM

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

--zbik--
5976
| 6 4 7
Lodz, Poland
--zbik--


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

 

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/7/15, 4:19 AM
Seen: 600 times
Last updated: 3/27/15, 8:10 PM