2

In Druapl 8, how can i change the date format used in a view (see screen shot)

enter image description here

The view uses a replacement pattern {{ created }} == Authored on

and i don’t find where to modify the format. I’ve checked :

configuration / regional and language / date and time formats

But i don’t know how to use the custom format with {{ created }} in the view.

Could it be done in admin without modify template file ?

3 Answers 3

2

In Drupal 8, fields use the normal field formatters. In your list of Fields in the View, click on Content: Authored on (Authored on). You can change the Formatter to what you want (same choices as Manage Display on the content type configuration). You can then configure other options, like the format.

If you already are using {{ created }} in the template, and you need it in a different format for a replacement pattern / token, then add Content: Authored on (Authored on) again to the list of Fields, but exclude it from display. Reorder things, so this appears before the field you need it as a token. Configure it as above. Then, where you need the token, you should have {{ created_1 }}, which is the duplicate you just made.

1
  • Thanks, it was simple but i was not familiar with this. Commented Feb 17, 2017 at 16:22
0

I havent try, but if you can use some twig maybe with something like that:

{{created|format_date("name_of_your_format")}}

Or

{{created.value|format_date("name_of_your_format")}}

maybe try to inspect that 'created' variable with a {{dump(created)}} Hope, it helps.

0

If you absolutely want a solution that involves replacement patterns, I've found that you need to use the raw value and run it through a conversion to UNIX time if you want to use a Drupal date format.

For example, I have a view of media that have an "Imported date" field (field_date_imported):

{{ field_date_imported__value | date("U") | format_date("medium") }}

I'm using this to add a "title" attribute to a span wrapping a "Time ago" date, so that the user can hover over the date to get the actual timestamp.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.