26

I'd like to change the date format that is used for articles in Drupal 8. It looks like it uses the Default medium date format by default. I added my own format at admin/config/regional/date-time, but I can't find where to edit the format that articles use.

Is there a way to do this from the CMS or does this need to be done in code?

10 Answers 10

49

So after spending more time than I care to admit looking into this, I've come up with two solutions to this.

Solution 1

One way to do it is with the date function that darol100 mentioned in his answer. Modifying his example for my case, in my node--article.html.twig file, I changed:

{% trans %}Submitted by {{ author_name }} on {{ date }}{% endtrans %}

to

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

The advantage to this approach is that it's easy and quick. The disadvantage is I'm not using Drupal's built-in date format system.

Solution 2

In order to use Drupal's date format system, I first created my custom format at admin/config/regional/date-time. Then I edited node--article.html.twig as follows:

{% set createdDate = node.getCreatedTime|format_date('my_custom_format') %}
{% trans %}Submitted by {{ author_name }} on {{ createdDate }} {% endtrans %}

This assumes I named my custom format "My Custom Format", which results in the machine name my_custom_format.

While this solution requires an extra step, I feel it's the more Drupal way to do it.

I learned about it on this page about Drupal's Twig Filters.

2
  • Great answer! For some reason, my custom date format isn't working and I'm getting what appears to be a literal interpretation of the string that my custom format name spells. I copied the machine name directly from my config just to be sure I didn't make a typo. I assume you've tried this feature? For "article_short_date", I'm getting: "pmWed, 20 May 2020 14:20:00 -050031202020-05-20T14:20:00-05:00WednesdayAmerica/Chicago_00022020Wed, 20 May 2020 14:20:00 -050031_20pm31America/Chicago" Commented Jun 3, 2020 at 4:56
  • Oops, I didn't realize solution 2 changes to a different method, from date() to format_date() Commented Jun 3, 2020 at 16:40
11

If you want to change the date format base on article content type level. You can edit/add the following code post.published_at|date and use a filter to alter the date format.

node--article.html.twig

{# Here are few example #}
{{ post.published_at|date(format='j  F  Y') }} {# Output = 10 March 2001#}
{{ post.published_at|date(format="F j, Y, g:i a") }} {# Output = March 10, 2001, 5:16 pm#}
{{ post.published_at|date(format='m/d/Y') }} {# Output 10/3/2001 #}

Twig use the same naming code as php date format. For more information about twig date formats visit - http://twig.sensiolabs.org/doc/filters/date.html

3
  • 1
    Thumbed up! darol100, Can this be done in TPL files in a same manner for someone with Drupal 7 site?
    – user16289
    Commented Jan 6, 2016 at 10:53
  • 1
    Yes, but is uses PHP template instead of twig syntax. It should looks something too this <?php print format_date($node->created, 'custom', 'm/d/y'); ?>. Commented Jan 6, 2016 at 10:57
  • Thanks darol100, this was quite useful in helping me learn about Twig filters. Ultimately, however, I went with using the format_date filter that Drupal provides (as I show in my answer).
    – Chris
    Commented Jan 7, 2016 at 17:47
10

I'm more for Chris's solution 2. As he says it IS more the drupal way... but it's not the best option yet.

format_date() has been deprecated and will be removed before Drupal 9... meaning that one day, you'll do a drupal update and this will break.

Instead, it's better to do this in your .theme file in a preprocess function.

function [theme_name]_preprocess_node(&$variables) {
  $variables['date'] = \Drupal::service('date.formatter')->format($variables['node']->getCreatedTime(), 'my_custom_format');
}
4

It is also possible to find the date format already in use, and just change that in the UI. Just compare what you see on the page with the date formats in admin/config/regional/date-time/

If you change eg "Default medium date" it will change the dates using this format without touching any template/twig files.

Date formats can also be set/translated per language for multi language setup.

4
  • This is the best answer!
    – andrew-e
    Commented Aug 6, 2019 at 0:55
  • This did not work for me. Default medium date had no effect on the date display although it was exactly the same format. I have also a "fallback" date format that I can't edit nor delete. Commented Jun 28, 2022 at 16:05
  • I would guess that the date format is overwritten by a twig template or views, or you are facing some language problems mentioned here drupal.org/node/2896235
    – eye-wonder
    Commented Aug 8, 2022 at 14:13
  • I had to change the Olivero medium format date to get this method to work.
    – pmagunia
    Commented Jul 13 at 20:35
2

An observation about what Duke said:

format_date() has been deprecated and will be removed before Drupal 9...

Yes, Drupal function format_date() defined in core/includes/common.inc is, in fact, deprecated. But, when you use the twig filter format_date() you are NOT calling that Drupal function, instead of that you are using the twig filter defined by Drupal in TwigExtension::getFilters(), and this filter calls DateFormatter::format().

Hence, using the twig filter format_date() is safe and Chris's solution 2 is fine.

2

You can customize the format in node.html.twig like this:

{{ node.createdtime|date(format="Y-m-d") }}
1

You can create your own Submitted By field by using Display Suite to create a Token Field.

In Drupal 8 /structure/ds/fields

Then add a field using html and tokens like...

By [node:author] on [node:created:custom:F j, Y]
0

To be sure that I change only the characters of the date but not any surrounding HTML elements, I use the following hook:

function [theme_name]_preprocess_field(array &$variables) {
  if ($variables['field_name'] === 'created') {
    $timestamp = $variables['element']['#items']->value;
    $markup = \Drupal::service('date.formatter')->format($timestamp, 'html_date');
    $variables['items'][0]['content']['#markup'] = $markup;
  }
}

Replace [theme_name] with the name of your theme and html_date with your desired time format, one of those you find under /admin/config/regional/date-time.

-1

You can have all the formats PHP for Date and Time

// Assuming today is March 10th, 2001, 5:16:18 pm, and that we are in the
// Mountain Standard Time (MST) Time Zone

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.
$today = 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 (the MySQL DATETIME format)


{% set createdDate = node.getCreatedTime|date('F j, Y, g:i a') %}
<div class="newsSingleAuthorDate">
      {% trans %}Submitted by <span>{{ author_name }}</span>  on {{ createdDate }} {% endtrans %}
</div>
-2

I used the following code for node--article--full.html.twig :

<li class="post-date">
    {% set day = node.createdtime|date("d") %}
      <span class="day">{{ day }}</span>
    {% set month = node.createdtime|date("M") %}
        <span class="month">{{ month }}</span>
 </li>


{% set date = node.createdtime|date("M d, Y") %}
   <span>{{ date }}</span>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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