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

function my_module_form_submit($form, FormStateInterface $form_state) {
  if (!$profile->field_someother_field->isEmpty()) {
    $date = new DrupalDateTime();
    $date->setTimezone(new \DateTimezone(DATETIME_STORAGE_TIMEZONE));
    $string = $date->format(DATETIME_DATETIME_STORAGE_FORMAT);
    $form_state->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?

2 Answers 2


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

  • I tried that as well and it still throws an exception :/
    – zerolab
    Nov 24, 2016 at 13:56

I've been working on a similar problem, and the following kind of thing works for me:

$form_state->setValue('field_date_updated_details', [['value' => $date]]);

As another respondent noted, I'm not using a string, but the actual DrupalDateTime object, and setting the 'value' inside that nested array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.