Drupal - How to print custom date format in Twig?

Try to use this -

{{ item.content.field_publication_date|date("M") }}

But, I think you will get your solution in

{{ item.content|date("M") }}

Where item.content contains the date string inside field--node--field-publication-date.html.twig.


First, create the custom date format my_custom_date_format in the configuration section of the drupal admin.

Second, use the format_date Twig filter, like here:

{{ node.created.value|format_date('my_custom_date_format') }}

It might be necessary to convert the date to a unix timestamp, first. That will look like this:

{{ node.my_date_field.value|date('U')|format_date('my_custom_date_format') }}

The \Drupal\datetime\Plugin\Field\FieldType\DateTimeItem has a value and a date property. The value is UTC and the date is computed. Use the date property to avoid having to deal with the timezone, and you can use a created format or a custom one:

{{ node.field_publication_date.date|format_date('custom', 'F j, Y') }}

Date format in Drupal:

{{ node.field_publication_date.date|format_date('my_custom_date_format') }}