Drupal 8 - Change article date format

By xngo on June 15, 2019

In Drupal 8, there are multiple ways to change the date format of a content type. In this tutorial, I will use the simplest way. For simplicity, I'm going to use Article content type for this tutorial. You can apply the same principle to all other content type. Simply use the machine name of your content type instead of article.

  1. Copy ./core/modules/node/templates/node.html.twig into the templates folder of your theme(e.g. For my case, it is ./themes/opwtheme/templates/).
  2. Rename node.html.twig to node--article.html.twig so that changes will only be applicable to Article content type.
  3. Open node--article.html.twig and replace
{% trans %}Submitted by {{ author_name }} on {{ date }}{% endtrans %}`

with

{% set createdDate = node.getCreatedTime|date('F j, Y') %}
{% trans %}By {{ author_name }} on {{ createdDate }} {% endtrans %}

Finally, clear all your cache.

Additional variations of date format

date("F j, Y, g:i a");                 // March 10, 2001, 5:16 pm
date("m.d.y");                         // 03.10.01
date("j, n, Y");                       // 10, 3, 2001
date("Ymd");                           // 20010310
date('h-i-s, j-m-y, it is w Day');     // 05-16-18, 10-03-01, 1631 1618 6 Satpm01
date('\i\t \i\s \t\h\e jS \d\a\y.');   // it is the 10th day.
date("D M j G:i:s T Y");               // Sat Mar 10 17:16:18 MST 2001
date('H:m:s \m \i\s\ \m\o\n\t\h');     // 17:03:18 m is month
date("H:i:s");                         // 17:16:18
date("Y-m-d H:i:s");                   // 2001-03-10 17:16:18

Reference

About the author

Xuan Ngo is the founder of OpenWritings.net. He currently lives in Montreal, Canada. He loves to write about programming and open source subjects.