<p>The output format for <code>DateTime</code> attributes is hard-coded in the <code>craft\base\Element</code> class, so there's no configuration you can use to change it. <a href="https://github.com/craftcms/cms/blob/3.7.30.1/src/base/Element.php#L3917-L3924" rel="nofollow noreferrer">See the source code here</a> for details.</p>
<p>You can, however, use events to overwrite the HTML for any attribute you wish. You're gonna need to create a <a href="https://craftcms.com/docs/3.x/extend/module-guide.html" rel="nofollow noreferrer">custom module</a> for this (don't worry, this is pretty straightforward). The event you're looking for is <code>craft\base\Element::EVENT_SET_TABLE_ATTRIBUTE_HTML</code>. You can find example code for event hooks <a href="https://craftcms.com/docs/3.x/extend/events.html#event-code-generator" rel="nofollow noreferrer">here</a>.</p>
<p>In this case, you're probably looking for the <code>postDate</code> attribute. Using the source code linked above as a starting point, here's a hook to modify the output format for the <code>postDate</code> attribute:</p>
<pre class="lang-php prettyprint-override"><code>use craft\base\Element;
use craft\events\SetElementTableAttributeHtmlEvent;
use craft\helpers\Html;
use craft\i18n\Locale;
use DateTime;
use yii\base\Event;

Event::on(
    Element::class,
    Element::EVENT_SET_TABLE_ATTRIBUTE_HTML,
    function (SetElementTableAttributeHtmlEvent $e) {
        $attribute = $e-&gt;attribute;

        // only modify the `postDate` attribute
        if ($attribute !== 'postDate') return;

        $value = $e-&gt;sender-&gt;{$attribute};
        if ($value instanceof DateTime) {
            $formatter = Craft::$app-&gt;getFormatter();
            $e-&gt;html = Html::tag('span', $formatter-&gt;asDatetime($value, Locale::LENGTH_LONG), [
                'title' =&gt; $formatter-&gt;asDatetime($value, Locale::LENGTH_FULL),
            ]);
        }
    }
);
</code></pre>
<p>You can find the available formats (<code>LENGTH_*</code> constants) <a href="https://github.com/craftcms/cms/blob/3.7.30.1/src/i18n/Locale.php#L185-L208" rel="nofollow noreferrer">here</a>. Of course, you can also use any custom format you like.</p>
<p>If you want this to apply to ALL attributes that have a <code>DateTime</code> value, leave out the condition that checks if the attribute name is <code>postDate</code>. If you want to only apply this hooks for a particular element type, attach it to the appropriate sub-class instead (for example, <code>craft\elements\Entry</code> instead of <code>craft\base\Element</code>).</p>

CC BY-SA 4.0
2022-01-28T13:54:21.473Z
2022-01-28T13:54:21.473Z
MoritzLost
<p>We need to display time along with the date in the admin listing pages. I do know that the tooltip will show the time. But the client needs the time to be displayed on the listing itself. Is there an option in craft CMS to enable this?</p>

<craft3><control-panel><admin>
Display time along with date in admin listing page
# Display time along with date in admin listing page

(Asked by: username_0 on 2022-01-28)

We need to display time along with the date in the admin listing pages. I do know that the tooltip will show the time. But the client needs the time to be displayed on the listing itself. Is there an option in craft CMS to enable this?

**Tags:** craft3, control-panel, admin


---

## 1 answers

### Answer by username_1 on 2022-01-28. Score: 3 | Accepted answer

The output format for DateTime attributes is hard-coded in the craft\base\Element class, so there's no configuration you can use to change it. See the source code here for details.
You can, however, use events to overwrite the HTML for any attribute you wish. You're gonna need to create a custom module for this (don't worry, this is pretty straightforward). The event you're looking for is craft\base\Element::EVENT_SET_TABLE_ATTRIBUTE_HTML. You can find example code for event hooks here.
In this case, you're probably looking for the postDate attribute. Using the source code linked above as a starting point, here's a hook to modify the output format for the postDate attribute:
use craft\base\Element;
use craft\events\SetElementTableAttributeHtmlEvent;
use craft\helpers\Html;
use craft\i18n\Locale;
use DateTime;
use yii\base\Event;

Event::on(
    Element::class,
    Element::EVENT_SET_TABLE_ATTRIBUTE_HTML,
    function (SetElementTableAttributeHtmlEvent $e) {
        $attribute = $e->attribute;

        // only modify the `postDate` attribute
        if ($attribute !== 'postDate') return;

        $value = $e->sender->{$attribute};
        if ($value instanceof DateTime) {
            $formatter = Craft::$app->getFormatter();
            $e->html = Html::tag('span', $formatter->asDatetime($value, Locale::LENGTH_LONG), [
                'title' => $formatter->asDatetime($value, Locale::LENGTH_FULL),
            ]);
        }
    }
);

You can find the available formats (LENGTH_* constants) here. Of course, you can also use any custom format you like.
If you want this to apply to ALL attributes that have a DateTime value, leave out the condition that checks if the attribute name is postDate. If you want to only apply this hooks for a particular element type, attach it to the appropriate sub-class instead (for example, craft\elements\Entry instead of craft\base\Element).


---

