How can I hide the "day" in a date field? I have a date field, but I only want to collect the year and month, not the day.  How can I hide the day part?

There are instructions for Drupal 7, but I am using Drupal 8.
Note that the day is required.

This means there are some difficulties:


You need to set the value for the day field (otherwise the form will fail validation).  In this example, we will set it to the 15th of the month.
You can't use unset() or #access: FALSE because this will cause the form to have no value and fail validation.


You can do this in an #after_build callback.

function MYMODULE_form_MYFORM_alter(&amp;$form, FormStateInterface $form_state, $form_id) {
  $form['#after_build'][] = 'MYMODULE_date_remove_day_after_build';
}

function MYMODULE_date_remove_day_after_build($form, &amp;$form_state) {
  $form['field_MYFIELD']['widget'][0]['value']['day']['#value'] = '15';
  $form['field_MYFIELD']['widget'][0]['value']['day']['#attributes']['class'][] = 'element-hidden';
  return $form;
}


And then in your theme .css, add this class:

.element-hidden {
  display: none !important;
}