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' => 'datetime',
  '#title' => t('My date'),
  '#default_value' => $config->get('mydate')

The form submission handler where the error occures:

$config->set('mydate', $form_state->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:

  type: config_object
  label: 'fancy module settings'
      type: datetime
      label: 'My date'

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

PS: using Drupal 8.2

2 Answers 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']

  • Could you please clear up how to convert a Drupal datetime into a timestamp and which config data type I have to use? This would complete your answer :) Dec 1, 2016 at 8:27
  • Updated with sheets to your questions
    – Eyal
    Dec 4, 2016 at 23:52
  • 1
    According to the example commented inside the datetime class I've used '#default_value' => new DrupalDateTime($config->get('mydate')). DrupalDateTime is an extension of DateTimePlus and works like charm in this case. Jun 28, 2017 at 12:35

Here's some actual code based on Eyal & Philipp's work in the other answer.

use Drupal\Core\Datetime\DrupalDateTime;


$form['mydate'] = [
    '#type'          => 'datetime',
    '#title'         => t('My date') ,
    '#default_value' => new DrupalDateTime( $config->get( 'mydate' ) ),


$config->set( 'mydate', $values['mydate']->__toString() )->save();

config yaml:

mydate: '2018-09-12 09:00:00 Europe/Berlin'

Play with in console:

$ drush php
Psy Shell v0.9.8 (PHP 7.1.22 — cli) by Justin Hileman
>>> use Drupal\Core\Datetime\DrupalDateTime;
>>> $time = new DrupalDateTime('2018-09-12 09:00:00 Europe/Berlin');
=> Drupal\Core\Datetime\DrupalDateTime {#4897}
>>> $time->getTimestamp();
=> 1536735600

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.