6

I have a twig file with the following code in my Drupal 8.4.4 site:

<div class="value col-md-12">{{ node.field_date.value }}</div>

And in my content type, manage display option, I have it formatted like this: enter image description here

But on the website, it shows the date as

2019-01-16T23:00:00

What am I missing?

8

You are printing the node value directly, which is bypassing the field formatter and other things (since you are not using the render array).

Do this instead:

{{ content.field_date }}

This will render normally and ensure field formatter settings are respected.

  • This is much better than my answer! – hotwebmatter Jan 7 at 15:19
1

Try piping the value to twig's date function:

https://twig.symfony.com/doc/2.x/filters/date.html

That approach might give you something like this:

<div class="value col-md-12">{{ node.field_date.value | date("D M j G:i T") }}</div>
  • 1
    I guess technically this would work, but you lose the ability to change the format on the fly from the admin. It would also print the div out, regardless of if there is a value in the field or not. – Kevin Jan 7 at 14:44
  • 2
    Funny enough this didn't work, I was trying this before until someone else pointed me to the fact that the formatting should work above. Thanks for posting! – David Lozzi Jan 7 at 15:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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