Aiswarya
Aiswarya

Reputation: 113

Odoo - Add (AM/PM) in datetime in qweb report

<span t-field="o.date_order"/>

which gives output as:

04/11/2017 09:48:35

But how can I get output like:

04/11/2017 09:48 AM

Upvotes: 1

Views: 3369

Answers (2)

Mayur Vora
Mayur Vora

Reputation: 942

Hello Aiswarya,

Try this below code,

1. Using Template,

If your o.date_order field return this format type 10/10/2017 10:10 AM DateTime with AM/PM so use below code otherwise gives the error.

<td>
    Order Date:
    <t t-esc="time.strftime('%d/%m/%Y %H:%M %a',time.strptime(o.date_order,'%d/%m/%Y %H:%M %a'))" />
</td>

2. You can also be using create function in python,

If you have multiple date fields use same or different date format so python function use is better because improve Reusability of code.

1. First your report.py file inside write below code,

def __init__(self, cr, uid, name, context=None):  
    super(class_name, self).__init__(cr, uid, name, context=context)  

    self.localcontext.update({  
        'format_date':self.format_date  
    }) 

def format_date(self,date):
        if date:
           return str(datetime.strptime(o.date_order, '%d/%m/%Y %H:%M %a').strftime('%d/%m/%Y %H:%M %a'))

2. Second your report_view_template.xml file inside write below code,

<td>
    Order Date:
    <t t-esc="format_date(o.date_order)" />
</td>

I hope my answer is helpful. If any query so comments, please.

Upvotes: 0

mahendra kamble
mahendra kamble

Reputation: 1395

try this

<span t-field="o.date_order" t-field-options='{"format": "MM dd y h:mm a"}'/>

Upvotes: 3

Related Questions