Search code examples
pythonpython-babel

How to set format as hour in babel.dates.format_date?


odoo ERP regroup by day as default :

data[k] = babel.dates.format_date(v, format="dd MMM yyyy", locale=context.get('lang', 'en_US')).

what i want to do is to change the format date from day to hour format.

I try to change the previous code line:

data[k] = babel.dates.format_date(v, format="HH dd MMM yyyy", locale=context.get('lang', 'en_US'))

but this return an error:

 AttributeError: 'datetime.date' object has no attribute 'hour'

TO clarify i want to create custom date/time pattern, in which the hour must appear.


Solution

  • You may need datetime object for hour not just date. try

    babel.dates.format_datetime

    See: http://babel.pocoo.org/docs/api/dates/