How to get the value of a datetime when form submitted 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' =&gt; 'datetime',
    '#title' =&gt; $this-&gt;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-&gt;getValue('timesave');


This is giving an array some protected methods.
You can use the method getTimeStamp() to store the timestamp like this :

$form_state-&gt;getValue('timesave')-&gt;getTimeStamp();


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

$form_state-&gt;getValue('timesave')-&gt;format("Y-m-d H:i:s");