This worked for me to get and set the date field value using presave hook based on content type /**
- Implements hook_entity_presave(). */
function YOUR_MODULE_global_entity_presave(Drupal\Core\Entity\EntityInterface $entity) { if ($entity->bundle() == 'blog') { $published = $entity->get('created')->value; $entity->set('field_published_date', date('Y-m-d\TH:i:s', $published)); } }
/**
* Implements hook_entity_presave().
*/
function YOUR_MODULE_global_entity_presave(Drupal\Core\Entity\EntityInterface $entity)
{
if ($entity->bundle() == 'blog') {
$published = $entity->get('created')->value;
$entity->set('field_published_date', date('Y-m-d\TH:i:s', $published));
}
}