Comparing node creation and modification dates in twig I want to compare a node's creation date and modified date in Twig, and it drives me bonkers. I've looked at various examples and snippets as part of this, this, this or this external source.

Using Drupal 8, Bootstrap 3, Bootstrap Layouts, Display Suite, and a custom  sub-theme, I have a twig template customising what is going to be displayed and how. This custom template is a copy of the default bootstrap layouts 1 column template.

Within the template, I can access and print the dates using this:

{{ main.content.node_changed_date }}
{{ main.content.node_post_date }}


and all is well.

However, I want to compare the dates in my template like so:

{% if main.content.node_post_date|date('Y-m-d') &gt; main.content.node_changed_date|date('Y-m-d') %}
    {{ main.content.node_post_date }}
{% endif %}


But this barks at me with the following error:

The website encountered an unexpected error. Please try again later.
TypeError: DateTime::__construct() expects parameter 1 to be string, array given in DateTime-&gt;__construct() (line 455 of /home/michel/devel/vendor/twig/twig/lib/Twig/Extension/Core.php).

DateTime-&gt;__construct(Array, Object) (Line: 455)
twig_date_converter(Object, Array, Object) (Line: 380)
twig_date_format_filter(Object, Array, 'Y-m-d') (Line: 77) 


Yes, I use Composer to manage my installation.

What am I doing wrong?

Pulling my hairs

Michel
You can't compare render arrays. To compare fields better use raw values stored in the node:

{% if node.getCreatedTime &gt; node.getChangedTime %}
    ...
{% endif %}


(The comparison doesn't make much sense, probably you want to swap both dates)

The node object is available in node and page templates. In other templates you might find it in a deeper level. If not and the node is displayed as main content you can add it in preprocess:

  if ($node = \Drupal::routeMatch()-&gt;getParameter('node')) {
    $variables['node'] = $node;
  }