How can I format date in Odoo 10 QWeb report?
Asked Answered
P

6

8

t-field-option is not working.

I have tried

<span t-field="o.date_invoice" t-field-options='{"format": "MM/dd/yyyy"}'/>
Personate answered 15/12, 2016 at 14:25 Comment(0)
S
8

Instead of using

<span t-field="o.date_invoice" t-field-options='{"format": "MM/dd/yyyy"}'/>

use

<span t-field="o.date_invoice" t-options='{"format": "MM/dd/yyyy"}'/>

Hope that helps!

Strickland answered 20/11, 2018 at 6:39 Comment(2)
In v12, the date/datetime fields are python date/datetime objects and not string representations. The following python formatting will work in v12: <span t-esc="o.date_invoice.strftime('%m/%d/%Y')"/> . odoo.com/groups/technical-62/technical-56392463Marylandmarylee
@TirthaR no it won't work as expected if you run odoo in different languages, strftime format based on the locale and not an arbitrary language.Markel
M
5

For those who arrive here from search engines, you can control display of date in form fields using widgets.

<field name="date_planned" widget="date"/>

or,

<field name="date_planned" widget="datetime"/>

In v12, the date/datetime fields are python date/datetime objects and not string representations. The following python formatting will work in v12 reports:

<span t-esc="o.date_invoice.strftime('%m/%d/%Y')"/>

https://www.odoo.com/groups/technical-62/technical-56392463

Marylandmarylee answered 13/11, 2018 at 6:9 Comment(3)
This is exactly what I was looking for! Odoo 12, in the when creating a purchase order in the 'Purchases' app, the datetime widget was adding too much information! I tried t-options='{"format": "yyyy/MM/dd"}' but that didn't work. Using widget="date" solved the problem. Thanks!Stereotomy
what does one have to type in order to get the month spelled out instead of it being just numbers?Semple
Try MMM or MMMM.Marylandmarylee
F
3

To display localized date strings. Try the following:

<span t-field="o.date_invoice" t-options="{&quot;widget&quot;: &quot;date&quot;}" />
Fencesitter answered 21/6, 2018 at 0:17 Comment(1)
Note to Editors: Please DO NOT EDIT &quot; as this is how it should be added in the qweb editor, thanksFencesitter
R
2

To delete the time section:

<span t-field="o.date_invoice" t-field-options='{"widget": "date"}'/>

Use t-field-options instead of t-options

Do not change the position of the quotes in t-field-options

This code respects the format date according to lang/country.

Riotous answered 23/10, 2019 at 12:32 Comment(0)
P
0

Try this.

<span t-esc="datetime.datetime.strptime(o.sale_id.confirmation_date, '%Y-%m-%d %H:%M:%S').strftime('%B %d,%Y')"/>

My Output is: May 28,2018

Plauen answered 28/5, 2018 at 8:54 Comment(1)
Error to render compiling AST TypeError: strptime() argument 1 must be str, not datetime.date I get this error. Can you pls helpStucker
R
0

According to my experience, you have used the correct way to format Qweb date, but sometimes there is problem in other thing and odoo gives error somewhere else. Hope trying this code may be helpful.

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

also use this code

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

You can also do one thing as formatting the date variable in the model itself and then show it on your QWeb report.

Rubella answered 15/7, 2018 at 18:8 Comment(1)
I think we should use t-options instead of t-field-optionsSeddon

© 2022 - 2024 — McMap. All rights reserved.