Looking for a way to add my own date formatter in view. Actualy have the "default" and "Time ago" one, but I want to had another one to display date has FB do in the notification feed :

For today notification : Today, 09:33

For Yesterday notification : Yesterday, 10:36

For this week notification (except today and yesterday) : Mon. 06:34

All other notification : 09/12/17, 15:59

Is this possible ? Or maybe in an other way than add something in view module ?

Working on Drupal 8.2.0 version.

  • yes i think there is an option in views date field to add custom date formatter. Dec 14, 2016 at 16:49

2 Answers 2


In Drupal 8, views field output uses the field formatters that entities use (Drupal 7 uses its own plugins for this).

The "Today", "Yesterday", "Monday", other functionality cannot be handled by the standard field formatters.

Drupal 8 makes it easy, though, to make new formatters. Make a custom module, and extend DateTimeDefaultFormatter. Inside this class, you would override the formatDate() method (and possible the defaultSettings() one, depending on how fancy you want to be. The method would look something like this

protected function formatDate() {
  $timezone = $this->getSetting('timezone_override') ?: $date->getTimezone()->getName();

  if ($date is today) {
    $output = $this->t('Today, @formatted', [
      '@formatted' => $this->dateFormatter->format($date->getTimestamp(), 'custom', 'H:i', $timezone != '' ? $timezone : NULL);
  else if ($date is yesterday) {
    $output = $this->t('Yesterday, @formatted', [
      '@formatted' => $this->dateFormatter->format($date->getTimestamp(), 'custom', 'H:i', $timezone != '' ? $timezone : NULL);
  else if ($date is this week) {
    $output = $this->dateFormatter->format($date->getTimestamp(), 'custom', 'D. H:i', $timezone != '' ? $timezone : NULL);
  else {
    $output = $this->dateFormatter->format($date->getTimestamp(), 'custom', 'm/d/y H:i', $timezone != '' ? $timezone : NULL);

  return $output;

You need to work out the logic to choose the proper format. Set the annotation on the formatter properly, and you can use this formatter in the view (or anywhere else).

This should give you a start.


You can make such functionality using a custom template. Please look my example:


    {% for item in items %}
      <div class="date">
        <span class="day">{{ item.content[0]['#markup']|date('j') }}</span>
        <span class="year">{{ item.content[0]['#markup']|date('M.Y') }}</span>
    {% endfor %}

As you can see, you can use some additional login in Twig.

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.