Set a datetime field configured as date-only I'm developing a module where I have to set a user profile date_field. I tried several methods to implement this but not one worked for me.
This is the code I am using.
private function updateUser($user_import,$uid = null) {
  if (!$uid) {
    return false;
  }

  $roles = $this-&gt;matchRoles($user_import-&gt;roles);
  $birthday = $this-&gt;birthdayDate($user_import-&gt;profile_geburtsdatum);
  $language = \Drupal::languageManager()-&gt;getCurrentLanguage()-&gt;getId();

  $user = \Drupal\user\Entity\User::load($uid);
  // $user-&gt;setPassword($this-&gt;randomPassword());
  $user-&gt;setEmail($user_import-&gt;mail);
  $user-&gt;setUsername($user_import-&gt;name);
  $user-&gt;enforceIsNew(false);

  // Optional settings
  $user-&gt;set(&quot;init&quot;, $user_import-&gt;init);
  $user-&gt;set(&quot;langcode&quot;, $language);
  $user-&gt;set(&quot;preferred_langcode&quot;, $language);
  $user-&gt;set(&quot;preferred_admin_langcode&quot;, $language);

  foreach ($roles as $role) {
    $user-&gt;addRole($role);
  }

  $user-&gt;set(&quot;field_profile_geburtsdatum&quot;, array('#default_value' =&gt; $birthday));
  $user-&gt;activate();
  $user-&gt;save();

  drupal_set_message(&quot;UPDATE: User &quot; . $user_import-&gt;name . ' ( '. $user_import-&gt;mail . ')');
}

private function birthdayDate($import_date) {
  $date = null;
  if (!empty($import_date)) {
    $date = strtotime($import_date-&gt;day.'.'.$import_date-&gt;month.'.'.$import_date-&gt;year);
    $date = date('Y-m-d 00:00:00',$date);
    $date = new \Drupal\Core\Datetime\DrupalDateTime($date, 'Europe/Berlin');

    /* $date = array(
         'year' =&gt; date('Y',$date),
         'month' =&gt; date('m',$date),
         'day' =&gt; date('d',$date),
       );
     */
  }

  return $date;
}
Assuming field_profile_geburtsdatum is configured as a singular date-only datetime field, the following are all equivalent in the Field API (in order of my preferred usage):

$user-&gt;field_profile_geburtsdatum = '1999-12-31';
$user-&gt;field_profile_geburtsdatum-&gt;value = '1999-12-31';
$user-&gt;field_profile_geburtsdatum-&gt;set(0, '1999-12-31');
$user-&gt;set('field_profile_geburtsdatum', ['1999-12-31']);


The value you set should be a string in the DATETIME_DATE_STORAGE_FORMAT, which is 'Y-m-d', and not a DrupalDateTime object.  That is used internally by the field type.