title: Twig: how to check if a variable is a DateTime object
body: <p>I have to check if a variable is a <code>DateTime</code>object or a simple string to use it in my template.</p>
  
  <p>If the variable is a <code>DateTime</code>, I have to format it as a date; simply print it if is a string.</p>
  
  <pre><code>{% if post.date is DateTime %}
      {% set postDate = post.date|date %}
  {% else %}
      {% set postDate = post.date %}
  {% endif %}
  
  &lt;p&gt;Il {{ postDate }}
  </code></pre>
  
  <p>I think I should do this using a <a href="http://twig.sensiolabs.org/doc/advanced.html#id5" rel="nofollow noreferrer">Twig Test</a> (as also suggested in <a href="https://stackoverflow.com/a/29642662/1399706">this</a> StackOverflow Answer about <code>arrays</code>), but I don't well understand in which folder of my Symfony App I should put the code and how to register it in the application.</p>
  
  <p><strong>Once wrote the test, how can I use it in my Twig templates in Symfony?</strong></p>