Drupal 8 formatted date using computed field

Use a computed field to build a formatted date output from an event date daterange (startdate) and a event status field.

 

use Drupal\Core\Datetime\DrupalDateTime;

$field_date = $entity->field_date;
$field_status = $entity->field_event_status->entity->name->value;

$field_day = '01';
$field_month = 'Jan';
$field_date = $entity->field_date->start_date;

if (isset($field_date)) {
  $date = new DrupalDateTime($field_date, 'UTC');
  $formatted_date = \Drupal::service('date.formatter')->format($date->getTimestamp(), 'custom', 'M j');
  $pieces = explode(' ', $formatted_date);
  $field_month = $pieces[0];
  $field_day = $pieces[1];
}

$field_status_class = strtolower($field_status);
if ($field_status == 'Postponed') {
  $dst = array('TBD', ' ', $field_status_class);
} elseif ($field_status == 'Canceled') {
  $dst = array('X', ' ', $field_status_class);
} else {
 $dst = array($field_day, $field_month, $field_status_class);
}
$src = array('{{ 1 }}', '{{ 2 }}', '{{ 3 }}');

$field_month_day = str_replace($src, $dst, '<div class="event-day event-status-{{ 3 }}">{{ 1 }}</div><div class="event-month">{{ 2 }}</div>');

$html = '<div class="event-page-day event-day-color-{{ field_day }} col-xs-2 col-sm-1"><div class="">{{ field_month_day }}</div></div>';
$src = array('{{ field_day }}', '{{ field_month_day }}');
$dst = array($field_day, $field_month_day);

$value = str_replace($src, $dst, $html);