Displaying a date field may seem trivial in a Twig template in Drupal 8, but a difficulty can arise when this field must for example be displayed in two parts. For example for an Event content type, it may happen that we want to display the date and time in a different way, when these two pieces of information come from the same date field.
Here are three ways to display Date fields in a Twig template (for example a node template). All of these methods will give the same result.
Use the formatting settings of the field (set through the UI)
{{ content.field_event_date }}
Use the date filter of Twig and a defined Drupal date format
{{ node.field_event_date.value|date('U')|format_date('event_date') }}
In this example, I have defined via the Drupal interface a date format which has the machine name event_date.
Use the date filter of Twig
{{ node.field_event_date.value|date('d/m/Y') }}
Other possible manipulations with dates
Display the creation date of the content
{{ node.getCreatedTime()|format_date('event_date') }}
Compare the value of the date field with the current date
Check if the value is in the past:
{{ date(node.field_event_date.value) < date() == 1 }}
Check if the value is in the future:
{{ date(node.field_event_date.value) > date() == 1 }}