Monday, February 9, 2015

How to format a date while printing in rml or in openoffice report in OpenERP (Odoo)


a) Default Date format on the form is:
e.g. 09-02-2015 i.e. dd-mm-YYYY

b) Default Date format while printing in reports is:
e.g. 2015-02-09 i.e. YYYY-mm-dd

and Your Desired Format to be shown in the reports,
1) As it is there in the form i.e. 09-02-2015 (dd-mm-YYYY)
2) or 09-FEB-2015 (dd-Month-YYYY)

Following is the simple method to format a date while printing in rml or in openoffice report in OpenERP (Odoo):

1) Write following code in reports .py file
import time
from datetime import datetime
#Add this line in self.localcontext.update dictionary
self.localcontext.update {
'get_date_ddmmyyyy': self.get_date_ddmmyyyy,
'get_date_ddmonthyyyy': self.get_date_ddmonthyyyy ,
}

2) #code to format date in dd-mm-YYYY
def get_date_ddmmyyyy(self, datec):
     return datetime.strptime(datec, "%Y-%m-%d").strftime("%d-%m-%Y")

3) #code to format date in dd-Month-YYYY
def get_date_ddmonthyyyy(self, datec):
     return time.strftime('%d') + '-' + datetime.strptime(datec, '%Y-%m-%d').strftime('%b').upper() + '-' + time.strftime('%Y')

4) Now Call any of this method (as per your required date format) from your reports .rml file i.e.
<para style="P2"> [[ get_date_ddmmyyyy(your_date) ]] </para>
<para style="P2"> [[ get_date_ddmonthyyyy(your_date) ]] </para>

And you are done.

Thanks,
Ujwala H. Pawade

No comments:

Post a Comment