Creating date field with only month and year We are using the Form API in a custom module (and not an entity field), how can we easily set the date widget for just year and month data entry?
I've tried adding a #date_format attribute but no change:
$form['date_needed'] = array(
  '#type' =&gt; 'date',
  '#title' =&gt; $this-&gt;t('Date Needed'),
  '#date_date_format' =&gt; 'Y/m',
);

I have also tried  '#date_date_format' =&gt; 'd/m/Y' but the date widget still appears asking for YYYY-mm-dd.
Looking at the source code for core/Render/Element/Date.php and stepping though the code, I can see it's picking up this setting.
The drupal.org doc page has a comment that indicate this should work: https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Render%21Element%21Date.php/class/Date/8.2.x
Any ideas?
We can make this in a hook_form_alter.
$form['DATE_FIELD_MACHINE_NAME']['widget'][0]['value']['#date_date_element'] = 'month';
$form['DATE_FIELD_MACHINE_NAME']['widget'][0]['value']['#date_date_format'] = 'Y-m';

However, there is still a problem of this way:  instead of first day of the month, current day will be saved. I cannot figure out how to solve.