QWeb Reports Date Fields

LDML Formatting

Had a request today to display a date in a qweb report in a certain way.  It really isn't documented anywhere so now it is.

Turns out that all fields have a default widget.  For example an Integer field will assign an integer widget in report rendering if no other widget is specified.  These widgets are represented internally by ir.qweb.field.integer for example, and so on.  Essentially, they take the fields value, convert it to display html and then apply some attributes. It's all quite extensible, extend existing fields, create new ones.  Very nice and something I'll probably make more use of.

The part we are interested here is the conversion to html of our datetime value so that 7/23/2018 12:34:56 becomes 23 July 2018.

A quick grep of the code found that you could indeed just assign the date widget to a datetime field and it would convert to a date.  But nowhere but nowhere did it mention how to format it, and it isn't done in core so no code examples to look at.  For those used to Python formatting, unfortunately that won't help either.  After some digging it turns out that the format needs to be specified in LDML format

To make matters a little more confusing, the argument that formats the date is called date_format, however the widget expects it to be called format.  So we end up with

<p t-field="doc.date_order" t-options='{"widget": "date", "format": "d MMMM yyyy"}' />

Below is half of the LDML codes for months, apparently you need to use L's if not displaying with a day.  This is because some languages have different words for each.  Reading through the list on the link above, the formatting is very comprehensive.  Periods, including noon and midnight.  The day formatter could be day of week, of month of year.  Quarters etc.  Actually makes the widget more useful than using python datestring formatting and this widget will be my new goto for these kinds of operations.  Alternatively, rather than using strftime, importing tools.format_date will deliver the same as this widget.

Code
Display
Meaning
M
9, 12Numeric: minimum digits
MM
09,12
Numeric: 2 digits, zero pad if needed
MMM
Sep
Abbreviated
MMMM
September
Wide
MMMMM
S
Narrow

Setting Settings with Doodba
Using an Odoo shell script