How can I customize the format and time zone of datetime fields in my custom labels?

How can I customize the format and time zone of datetime fields in my custom labels?

While adding datetime fields to your custom labels, you may want to customize their format or time zone. In this article, we will show you how to do it.

Changing time zone

By default, datetime fields on custom labels will be rendered in UTC time zone. To modify time zone, click on the “Open Label View” button (this button is visible only in debug mode):

Find the field in the label content. For instance, in this example, the ‘Scheduled Date’ field from transfers is being used:

To adjust the display to the time zone of your current user, modify the field definition. Here’s an example of what that change might look like:

<t t-esc="doc.scheduled_date" t-options="{'widget': 'datetime'}"/>

If you wish to set a specific time zone, you can do so, like this:

<t t-esc="doc.scheduled_date" t-options="{'widget': 'datetime', 'tz_name': 'Europe/London'}"/>

Format

The standard display format for datetime fields is: 2023-08-03 14:58:20.647024. To adjust the datetime format to the default format you’ve established in Odoo, you should set a widget:

<t t-esc="doc.scheduled_date" t-options="{'widget': 'datetime'}"/>

You have the freedom to set any datetime format you prefer. Here’s how:

<t t-esc="doc.scheduled_date" t-options="{'widget': 'datetime', 'format': 'dd-MM-yyyy HH:mm:ss'}"/>

Keep in mind, for these changes to take effect, the ‘widget’ parameter is vital. Without it, other parameters will be disregarded, or you may receive an error message.

The finalized updated field definition:

<t
  t-esc="doc.scheduled_date"
  t-options="{'widget': 'datetime', 'format': 'dd-MM-yyyy HH:mm:ss', 'tz_name': 'Europe/London'}"
/>

Important notice

Please note that any changes in label content in Odoo will be overridden when you click ‘Update in Odoo’ button:

If you encounter any difficulties or have further inquiries, don’t hesitate to reach out to us at support@ventor.tech, and we’ll gladly assist you.

References

Related Posts
Leave a Reply

Your email address will not be published.Required fields are marked *