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.

2 Answers 2


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(&$form, FormStateInterface $form_state, $form_id) {
  $form['#after_build'][] = 'MYMODULE_date_remove_day_after_build';

function MYMODULE_date_remove_day_after_build($form, &$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;
  • This worked a charm. Thank you.
    – RominRonin
    Commented Nov 6, 2019 at 16:11

Not sure exactly but if I understand your question correctly...

One approach might be through templating. In your template file try printing this:

{{ content.field_name.0.value | date('F j - h:i a') }}

I am not sure what this would actually be, use {{ dump(content | keys) }} in your template file to see what array keys are available.

This method, you are able to apply the date filter to the field time value which allows you to use php date formatting... you can then render the timecode any way you want.

  • you can always just create a template file for that specific field... once the template file is rendering how you want it, that field will show up the same anywhere it is used in your site
    – Preston
    Commented Oct 11, 2017 at 21:22
  • This question is about how to hide the "day" part of a date select widget on the form display screen. The problem with using a template to do this is that you still need to give Drupal a value for the "day" or validation will fail. Commented Oct 12, 2017 at 2:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.