I want to display the datetime field on my Drupal 8 form with some date loaded from the database, but the front end always shows empty fields as if no data has been provided. I tried several combinations to default_value field, but none of them are showing any thing.

The code I am using is the following.

$form['dateopening'] = array(
  '#type' => 'datetime',
  '#title' => t('Opening Date'),
  '#default_value'=> date('Y-m-d H:i:s', strtotime($dateFromDB)),

Why doesn't it set the default value as expected?

  • add a format like : $format = 'Y-n-d'; then add default value like: '#default_value' => date('c'), Commented Feb 8, 2016 at 13:16
  • It is 'datetime' field not 'date' field which I wanted. It is not working.
    – suneel
    Commented Feb 11, 2016 at 5:23

1 Answer 1


The response is in this thread : on stackoverflow

$form['valid_dt'] = array(
    '#type' => 'datetime',
    '#default_value' => DrupalDateTime::createFromTimestamp($data->valid_dt),
    '#title' => $this->t('Valid'),      

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.