How can I format date in Odoo 8 QWeb report?
Asked Answered
A

3

9

The date in my Sales Order is currently showing as:

Fecha: 21/11/2014 16:59:15 

I want to show something like this:

Fecha: Surco, 21 de Noviembre del 2014

I tried using t-esc with strftime but that doesn't work:

<span t-esc="o.date_order.strftime('%Y')" />
Agle answered 21/11, 2014 at 22:56 Comment(0)
A
8

It seems like o.date_order it's not a datetime object but a string. Using the time module is the way to go:

<span t-esc="time.strftime('%A, %d %B %Y',time.strptime(o.date_order,'%Y-%m-%d %H:%M:%S'))"/>
Agle answered 24/11, 2014 at 15:30 Comment(1)
the date format in the t-field-options attribute uses LDML pattern, so {"format": "EEEE d MMMM y"} will get the desired result. unicode.org/reports/tr35/tr35-dates.html#Date_Format_PatternsIto
M
17

Try using:

<span t-field="o.date_order" t-field-options='{"format": "d MMMM y"}'/>

Result: 21 Noviembre 2014

Maddox answered 24/11, 2014 at 16:52 Comment(3)
Thank you! That's even better. But how can I format like: "Surco, 21 de Noviembre del 2014" using this method?Enchanter
I am also trying to do that. I still can not.Maddox
Sad, cannot get the format from the LANG variable (or from any), since t-field-options must be a strict JSON object (not a python dict).Mulderig
A
8

It seems like o.date_order it's not a datetime object but a string. Using the time module is the way to go:

<span t-esc="time.strftime('%A, %d %B %Y',time.strptime(o.date_order,'%Y-%m-%d %H:%M:%S'))"/>
Agle answered 24/11, 2014 at 15:30 Comment(1)
the date format in the t-field-options attribute uses LDML pattern, so {"format": "EEEE d MMMM y"} will get the desired result. unicode.org/reports/tr35/tr35-dates.html#Date_Format_PatternsIto
A
0

Hear You can also set the custom date formate using function

Add the function in your_report.py file

class member_branch_mov(report_sxw.rml_parse):
    def __init__(self, cr, uid, name, context):
        super(member_branch_mov, self).__init__(cr, uid, name, context)
        self.localcontext.update({
            'time': time,
            'get_formate_header_date':self._get_formate_header_date      
        })


    def _get_formate_header_date(self, objects):
        header_date=''
        if self.end_date:
            date = datetime.strptime(self.end_date,'%Y-%m-%d')
            header_date=date.strftime('%d  %B %Y')
        return header_date.upper()

hear self.end_date comes from the wizard field

Add in your report your_report_view.xml File

 <t t-if="get_formate_header_date(docs)"><span t-esc="get_formate_header_date(docs)" /></t>
Andrey answered 20/2, 2015 at 12:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.