How to save datetime field value in config? I have a custom module (mymodule) with a settings form. This form contains a datetime field (mydate) which I want to save in my modules config.

When I try to save this form I get an exception:


  Drupal\Core\Config\UnsupportedDataTypeConfigException: Invalid data type for config element mymodule.settings:mydate in Drupal\Core\Config\StorableConfigBase->validateValue() (line 160 of core/lib/Drupal/Core/Config/StorableConfigBase.php)


The form field:

$form['mydate'] = array(
  '#type' =&gt; 'datetime',
  '#title' =&gt; t('My date'),
  '#default_value' =&gt; $config-&gt;get('mydate')
)


The form submission handler where the error occures:

$config-&gt;set('mydate', $form_state-&gt;getValue('mydate));


I guess I have to set up my config schema to set up the "right type". Currently I have this one but it does not solve my issue:

mymodule.settings:
  type: config_object
  label: 'fancy module settings'
  mapping:
    mydate:
      type: datetime
      label: 'My date'


Any idea what I'm doing wrong or what is missing? 

PS: using Drupal 8.2
There is no schema for data type called datetime.

Easiest solution is to store the date as the timestamp string.

Converting a drupal datetime into a timestamp:
The datetime form element holds an array value that contains three keys: date, time, object.
The object holds an instance of the [\Drupal\Component\Datetime\DateTimePlus][1] class. It has a _toString method which means it can be converted to string via normal casting (i.e. (string) $value).

Setting a default value on a datetime field:
Too do that you will need to create a new [\Drupal\Component\Datetime\DateTimePlus][1] object and set it as the value of $element['#default_value']