create a date field with only month and year in my custom form?

160 Views Asked by At

I'm new to drupal, can anyone tell me how can I create a date field with only month and year in my custom form? I have found a module for it but I want that field in my custom module form.

I have tried many things with form and I'm expecting something like this

$form['filters']['report_month'] = array(
'#type' => 'date',
'#title' => t('Report Month'),
'#date_date_format' => 'm/Y',
'#date_date_element' => 'month',
'#default_value' => date("m-Y"),
);
1

There are 1 best solutions below

0
Mohitk0509 On

my job is done with this but if anyone has a better option please reply.

$form['filters']['report_month'] = array(
        '#type' => 'datetime',
        '#title' => t('Report Month'),
        '#date_date_format' => 'm-Y',
        '#date_date_element' => 'month',
        '#date_time_element' => 'none',
        '#default_value' => date("m-Y"),
    );