1

I am writing a custom module in which i am declaring a $form element as #type=> 'datetime'

The code for the buildForm method is

 $form['timesave'] = array(
    '#type' => 'datetime',
    '#title' => $this->t('Select Date and time'),
  );

Now my question is how do we get the value of the field on form Submit as i have to store the value in a configuration

I have tried with

$form_state->getValue('timesave');

This is giving an array some protected methods.

1

You can use the method getTimeStamp() to store the timestamp like this :

$form_state->getValue('timesave')->getTimeStamp();

Or you can use the format() function to store a formatting date.

$form_state->getValue('timesave')->format("Y-m-d H:i:s");
  • Yup. Basically, the return value from $form_state->getValue('timesave') is a DrupalDateTime object, so any of the public methods/properties will work. – mpdonadio Jul 31 '17 at 15:04
  • doesn't work like that... at least in Drupal 8. – Dazag Mar 14 at 12:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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