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') > 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->__construct() (line 455 of /home/michel/devel/vendor/twig/twig/lib/Twig/Extension/Core.php).

DateTime->__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



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

{% if node.getCreatedTime > 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()->getParameter('node')) {
    $variables['node'] = $node;
  • Thanks for the answer - I would need to dig deeper, but my Drupal knowledge is too limited. It is part of a view output that lists the 5 most recent notes of a given type, and displays them in a custom display mode.
    – MDr
    Jun 12 '18 at 15:25
  • Then you could use the node template node--content-type--custom-display-mode.html.twig.
    – 4k4
    Jun 12 '18 at 15:55
  • I need to use a copy of a bootstrap-layout template, as I am making use of a display suite dynamic token field to display the number of comments (is there another way to do that?)
    – MDr
    Jun 12 '18 at 15:59
  • In a node template {{ node.comment.comment_count }}
    – 4k4
    Jun 12 '18 at 16:11
  • Perfect, it works like a charm! - Out of curiosity, does Drupal know a third date, the publication date? Because sometimes it takes longer than a day to create a content node, and in that case a publication date (or the most recent one, in case of repeated publication and unpublication) would be more meaningful than the technical node creation date.
    – MDr
    Jun 13 '18 at 13:21

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.