By David Lozzi


2019-01-07 14:21:14 8 Comments

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?

2 comments

@Kevin 2019-01-07 14:33:47

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.

@hotwebmatter 2019-01-07 15:19:54

This is much better than my answer!

@David Lozzi 2019-01-07 15:27:48

D'oh, that got me again. Thank you!

@hotwebmatter 2019-01-07 14:30:31

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>

@Kevin 2019-01-07 14:44:32

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.

@David Lozzi 2019-01-07 15:28:10

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!

Related Questions

Sponsored Content

1 Answered Questions

Alter a date format in a view based on another date

  • 2017-05-27 01:32:06
  • Eric Gilson
  • 28 View
  • 0 Score
  • 1 Answer
  • Tags:   views datetime

3 Answered Questions

[SOLVED] Trying to render a custom date format with Twig

  • 2013-11-26 03:49:51
  • Danny Englander
  • 8992 View
  • 5 Score
  • 3 Answer
  • Tags:   datetime 8 theming

1 Answered Questions

0 Answered Questions

Restful API: Change date format from unixtime to ISO-8601

  • 2016-04-14 16:38:56
  • Harry L Respass
  • 17 View
  • 1 Score
  • 0 Answer
  • Tags:   database datetime 8

2 Answered Questions

[SOLVED] theming date format

1 Answered Questions

1 Answered Questions

Broken formatting while displaying both start and end date

  • 2013-07-17 14:28:53
  • Eugene Pavlov
  • 110 View
  • 0 Score
  • 1 Answer
  • Tags:   7 datetime

1 Answered Questions

[SOLVED] view to show content with future date

  • 2013-09-26 12:41:04
  • ruby
  • 743 View
  • 2 Score
  • 1 Answer
  • Tags:   views datetime

1 Answered Questions

[SOLVED] Custom Date Format Not Available to my Date Field

  • 2013-08-15 20:55:34
  • Rob Orr
  • 1441 View
  • 1 Score
  • 1 Answer
  • Tags:   7 views datetime

0 Answered Questions

Default date in date popup in drupal 6

  • 2013-04-26 11:56:15
  • user7282
  • 966 View
  • 1 Score
  • 0 Answer
  • Tags:   datetime

Sponsored Content