How to change DateTime field value on form submit I am trying to update a DateTime field in an altered form submit. Something like:

php
function my_module_form_submit($form, FormStateInterface $form_state) {
  if (!$profile-&gt;field_someother_field-&gt;isEmpty()) {
    $date = new DrupalDateTime();
    $date-&gt;setTimezone(new \DateTimezone(DATETIME_STORAGE_TIMEZONE));
    $string = $date-&gt;format(DATETIME_DATETIME_STORAGE_FORMAT);
    $form_state-&gt;setValue('field_date_updated_details', [$string]);
  }
}


But this fails with various messages (depending on whether I set the value as an array or directly as string).

Any advice?
The form value need to be an call DrupalDateTime, so you can directly pass $date as value.