Back to Top

How to add datetime type field to a custom form in Magento 2

Updated 29 February 2024

In this blog we will learn how to add a datetime type field to a fieldset in custom form at admin side in Magento 2.

We will not explain here basic form creation. If you are a beginner then you can check this blog .

After successfully created basic form. Now you can add following code into Form.php In App/Code/Webkul/Grid/Block/Adminhtml/Grid/Edit to add datetime field.

$fieldset->addField(
    'start_date',
    'date',
    [
        'name' => 'start_date',
        'label' => __('Start Date'),
        'id' => 'start_date',
        'title' => __('Start Date'),
        'date_format' => 'yyyy-MM-dd',
        'time_format' => 'hh:mm:ss',
        'required' => true            
    ]
);

It will create a datetime type field like below in admin form,

date field
calendar wdget

Thanks for reading. Feel free to comment if you face any issue.

Searching for an experienced
Magento 2 Company ?
Find out More
. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Back to Top

Message Sent!

If you have more details or questions, you can reply to the received confirmation email.

Back to Home