6

Try this code (at node.html.twig, Drupal 8.0.0-rc3):

{% set date = node.created|format_date('long') %}

And next:

{{ dump(date) }}

Return error:

Exception: The timestamp must be numeric. in Drupal\Component\Datetime\DateTimePlus::createFromTimestamp() (line 165 of core/lib/Drupal/Component/Datetime/DateTimePlus.php).

Drupal\Core\Datetime\DateFormatter->format(Object, 'long')
0

2 Answers 2

16

you are not using the actual timestamp. Here's what you should do:

{% set date = node.createdtime|format_date('long') %}

See? You only need to replace created with createdtime. Hope this helps.

5
  • Thx, this work.
    – Platon
    Commented Nov 12, 2015 at 11:36
  • And what variables, such as createdtime, from node? If I try {{ dump(node) }} — request is WSOD..
    – Platon
    Commented Nov 12, 2015 at 11:39
  • Use Kint. It's a sub module of the devel module.
    – star-szr
    Commented Nov 12, 2015 at 12:01
  • createdtime is a twig style shortcut for the getCreatedTime() method. node.created is a FIeldItemList object, you can get the same value by accessing the value field property with node.created.value1format... Only base fields have methods, configurable fields need to be access like above.
    – Berdir
    Commented Nov 12, 2015 at 21:36
  • {% set date = node.createdtime|format_date('long') %} did not work for me. I had to use: {% set date = node.createdtime|date('long') %}
    – Ben
    Commented Apr 18, 2017 at 11:04
5

Try in this way.

{% set date = node.created %}
{{ set date|date("m/d/Y") }}
  • Refer here to know more about filter date in TWIG.
  • Refer here to know more about date formats in php.

Hope this will help you.

Not the answer you're looking for? Browse other questions tagged or ask your own question.