cancel
Showing results for 
Search instead for 
Did you mean: 

Magento 2 - Create DataTime attribute for product

Magento 2 - Create DataTime attribute for product

I'm trying to create datatime attribute for product in Magento2. But it didn't creating with time only the date is displaying.
If anybody have created this already. If means please share..

2 REPLIES 2

Re: Magento 2 - Create DataTime attribute for product

@Priya Ponnusamy Please share your code, Which code Have  you tried for creating datetime product attribute.

If Issue Solved, Click Kudos/Accept As solutions. Get Magento insight from
Magento 2 Blogs/Tutorial

Re: Magento 2 - Create DataTime attribute for product

@Rakesh Jesadiya Thanks for responding..
Sure. I'm updating my code here.
InstallData.php

 $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]); $eavSetup->removeAttribute(\Magento\Catalog\Model\Product::ENTITY, 'course_start_datetime');
$eavSetup->addAttribute( \Magento\Catalog\Model\Product::ENTITY, 'course_start', [ 'group' => 'Custom Attribute', 'label' => 'Enable Start Date', 'type' => 'datetime', 'input' => 'date', 'input_renderer' => 'Velanapps\Test\Block\Adminhtml\Form\Element\Datetime', 'class' => 'validate-date', 'backend' => 'Magento\Catalog\Model\Attribute\Backend\Startdate', 'required' => false, 'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE, 'visible' => true, 'required' => false, 'user_defined' => true, 'default' => '', 'searchable' => true, 'filterable' => true, 'filterable_in_search' => true, 'visible_in_advanced_search' => true, 'comparable' => false, 'visible_on_front' => false, 'used_in_product_listing' => true, 'unique' => false ] );

And the block for input renderer is below:

 <?php
  namespace Velanapps\Test\Block\Adminhtml\Form\Element;
  use Magento\Framework\Data\Form\Element\Date;

  class DateTime extends Date {
       public function getElementHtml() {
$this->setDateFormat($this->localeDate->getDateFormat(\IntlDateFormatter::SHORT));
$this->setTimeFormat($this->localeDate->getTimeFormat(\IntlDateFormatter::SHORT)); return parent::getElementHtml(); } }