I have a standard Drupal date field 'My date' (field_my_date). I can get its value:
$node->field_my_date->value // returns: "2017-12-14T08:00:00"
But I want to get a formatted date string, which should look like: "14.12.2017".
If I analyse the field in the interactive shell (drush core-cli), then I see, that I can get the DateTimeFieldItemList object or DateTimeItem object:
>>> $node->field_my_date
=> Drupal\datetime\Plugin\Field\FieldType\DateTimeFieldItemList {#8836
0: Drupal\datetime\Plugin\Field\FieldType\DateTimeItem {#8841},
}
>>> $node->field_my_date[0]
=> Drupal\datetime\Plugin\Field\FieldType\DateTimeItem {#8841
value: [
"value" => "2017-12-14T08:00:00",
],
}
But I have not found any method to reformat the date string. Is there some possibility to define the format of the output date string? Or do I have to reformat the output string e.g. like this:
date("j.n.Y", strtotime($node->field_my_date->value));