Magento2 Backend – How to Add Time to Datepicker

datedatepickermagento2moduletime

I'am using magento 2.0.5 and I want to add date field with time which saves date as well as time in database for entity.

I had tried this code to add new field in my module.

$fieldset->addField('enddate', 'date', array(
        'name' => 'enddate',
        'label' => __('End date'),
        'title' => __('End date'),
        'format' => 'yy-mm-dd H:i:s',
        'input_format' => 'yy-mm-dd H:i:s',
        'required' => true,
            )
    );

but this gives me only date to select not time

enter image description here

Please help me.

Thanks.

Best Answer

You want to have the declaration of the field similar to the following;

$fieldset->addField(
    'date_to',
    'date',
    [
        'name' => 'date_to',
        'label' => __('Start Time'),
        'title' => __('Start Time'),
        'date_format' => $this->_localeDate->getDateFormat(\IntlDateFormatter::SHORT),
        'time_format' => $this->_localeDate->getTimeFormat(\IntlDateFormatter::SHORT),
        'class' => 'validate-date'
    ]
);

The important bits are the date_format and time_format keys.

Related Topic