Magento – create date and time attribute for product in magento 2

datetimemagento-2.1

I tried to create an attribute for product which has to be display date and time calender and that should be stored in database as date and time formate only.I struggled it for a lot but no luck.Can you please any one help me..
this is my script

$eavSetup->addAttribute(
        \Magento\Catalog\Model\Product::ENTITY,
        'xyz_start_time',
        [
        'group' => 'xyz',
        'type' => 'datetime',
        'backend' => '',
        'frontend' => '',
        'label' => 'Start Time',
        'input' => 'date',
        'class' => '',
        'source' => '',
        'global' => \Magento\Catalog\Model\ResourceModel\Eav\Attribute::SCOPE_GLOBAL,
        'visible' => true,
        'required' => false,
        'user_defined' => true,
        'default' => '',
        'searchable' => false,
        'filterable' => false,
        'comparable' => false,
        'visible_on_front' => false,
        'used_in_product_listing' => true,
        'unique' => false,
        'apply_to' => 'simple,configurable,virtual,bundle,downloadable'
        ]
        );

$eavSetup->addAttribute( \Magento\Catalog\Model\Product::ENTITY, 'xyz_end_time', [ 'group' => 'xyz', 'type' => 'datetime', 'backend' => '', 'frontend' => '', 'label' => 'End Time', 'input' => 'date', 'class' => '', 'source' => '', 'global' => \Magento\Catalog\Model\ResourceModel\Eav\Attribute::SCOPE_GLOBAL, 'visible' => true, 'required' => false, 'user_defined' => true, 'default' => '', 'searchable' => false, 'filterable' => false, 'comparable' => false, 'visible_on_front' => false, 'used_in_product_listing' => true, 'unique' => false, 'apply_to' => 'simple,configurable,virtual,bundle,downloadable' ] );

It is creating datepicker with date only not showing time on it.

Best Answer

One of the possible solutions is to use Modifiers. You would need to define a modifier in Vendor\Module\etc\adminhtml\di.xml and then create the modifier itself in Vendor\Module\Ui\DataProvider\Product\Form\Modifier\Datetime.php. The modifier should set time config for your attribute.

Please refer to this answer here for more info: Magento 2 Create Date with Time attribute for product

Related Topic