Even if content.field_my_date
contains a date like 2021-10-01, twigs date function outputs today's date when using {{ content.field_my_date|date("M.d.Y") }}
. Simply printing {{ content.field_my_date }}
to make sure the field isn't empty, outputs the correct date and label.
1 Answer
Before you need convert field field_my_date
form 2021-10-01
to timestamp
You can use hook_ENTITY_TYPE_view_alter to be convert it to timestamp
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
/**
* Implements hook_ENTITY_TYPE_view().
*/
function hook_ENTITY_TYPE_view_alter(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display) {
if($display->bundle()=='node_type'){
$field_date_timestamp = strtotime($entity->get('field_my_date')- >value);
$build['field_my_date'] = \Drupal::service('date.formatter')->format(
$field_date_timestamp, 'custom', 'M.d.Y');
}
}
in file twig node tempalte {{field_my_date}}