I'm in the process of porting one of my contrib themes to Drupal 8. For custom date formats on nodes, I have something like this in a node_preprocess function in the theme's template.php file in the Drupal 7 version:

$vars['thedate'] = format_date($node->created, "custom", "j");
$vars['themonth'] = format_date($node->created, "custom", "M");
$vars['theyear'] = format_date($node->created, "custom", "Y");

I render it with the following code.

  <?php print $thedate; ?> / <?php print $themonth; ?> / <?php print $theyear; ?>

In my Drupal 8 port, I've tried a similar approach in the theme's .theme file and then try to render them in node.html.twig as {{{ thedate }} / {{ themonth }} / {{ theyear }}}, but I'm getting a nasty error.

Twig_Error_Runtime: An exception has been thrown during the rendering of a template ("The timestamp must be numeric.") in themes/mytheme/templates/page.html.twig at line 210. in Twig_Template->displayWithErrorHandling() (line 279 of /site/core/vendor/twig/twig/lib/Twig/Template.php).

I looked to Twig date formats, and it seems you should simply attach a variable, such as {{ display_submitted |date("m/d/Y") }}. I tried that without a preprocess function but what outputs is 12/31/1969, which is not the date the node was created so I'm a bit lost here.

  • 2
    Note that this has nothing to do with Twig (apart from the unhelpful error handling), but I'm leaving the question as it is, as someone else might search for this. I'll expect we'll see a few similar ones, this isn't obvious if you're coming from 7.x. – Berdir Nov 26 '13 at 9:07

$node->created is a FieldItemList object.

You need to use either one of these.

$vars['thedate'] = format_date($node->created->value, "custom", "j");
$vars['thedate'] = format_date($node->getCreatedTime(), "custom", "j");

All node base fields (and most other entity types, some are still being worked on) have methods now, that are defined in NodeInterface.

  • 1
    Changed TypedData to FieldItemList and made the documentation link a bit more specific. – Berdir Nov 26 '13 at 7:56
  • Very interesting! Both of those work fine so thanks for that. I'm one of those who will be really struggling to wrap my arms around Drupal 8 (self taught, no CS degree) so I'm trying to start early to unravel some of this. The jury is still out whether I'll be up tot he task or not so I appreciate the help. – Danny Englander Nov 26 '13 at 14:03
  • Just curious, for comments, if I use $vars['created'] = format_date($comment->getCreatedTime(), "custom", "m / j / y");, that does not work, I need to use $vars['created'] = format_date($comment->created->value, "custom", "m / j / y"); so it seems inconsistent... – Danny Englander Nov 27 '13 at 15:02
  • Comment::getCreatedTime() will work eventually, see drupal.org/node/2028025 – tim.plunkett Nov 28 '13 at 5:36

Here is an approach that uses just Twig (no preprocess functions needed):

<p>{{ node.createdtime | date("d F Y") }}</p>
  • Excellent, the more I have been using Twig, the more I discover that preprocess functions are needed far less. – Danny Englander Mar 16 '16 at 17:02

For people who need a little more help understanding this, add the following to YOURTHEMENAME.theme file:

 * Implements template_preprocess_comment()
function YOURTHEMENAME_preprocess_comment(&$variables) {
  $comment = $variables['elements']['#comment'];
  $variables['YOUR_DATE_NAME'] = format_date($comment->created->value, "custom", "m / j / y");

Then in your theme, update the twig template (such as templates/comment.html.twig) to use the variable like so: {{ YOUR_DATE_NAME }}

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.