Theme colour switcher

Change format of comment created date - Drupal 8 & 9

I wanted to change the format of the comment submitted timestamp on my blog, and figured it could be done in Twig - but nope. There's an active issue on to allow the format to be changed in the UI, but until that's done, you need to use a theme preprocess function.

function THEMENAME_preprocess_comment(&$variables) {
  $date_formatter = \Drupal::service('date.formatter');
  $comment = $variables['elements']['#comment'];
  $variables['created'] = $date_formatter->format($comment->getCreatedTime(), 'date_time');

'date_time' is the machine name of a date format; you can create your own custom date format at /admin/config/regional/date-time, and pass it here.

You can then print that {{ created }} variable in your comment.html.twig template, along with the {{ author }} variable, in place of the standard {{ submitted }}. Here's what I have in my comment.html.twig:

<p class="comment--submitted">{% trans %}{{ author }} on {{ created }}{% endtrans %}</p>


Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.