Using a 'date' field in the FormAPI, is there something I can set to hide the day field from showing?


There isn't a setting for that, but you could do it with the #after_build option, as it will allow you to alter the form element after it has been processed (day, month and year fields have been created).


I think you could accomplish this using the widgets provided by the Date module, specifically the date_text widget:

$form['date'] = array(
  '#type' => 'date_text',
  '#title' => 'Date',
  '#date_format' => 'm/Y',
  '#default_value' => date('Y-m-d')

You can use the #date_format key to set the level of granularity you want for the element date.

Be aware the default value has to be in the Y-m-d format for some reason (can't dig out the link to the issue on drupal.org at the moment but I'll update the answer if I find it).

  • date_default_date() in date_api_elements.inc requires that the element has a default value. date('Y-m') works for this case as well, although any value longer than the "granularity" requires works. – chim Sep 19 '13 at 10:06
  • Very little on the internet for this problem, which must be an incredibly common use case. Thanks for posting. – chim Sep 19 '13 at 10:07

Here's a code example, close to the op request that may help other devs. Based on the answer by @Clive

$form['deadline'] = array(
  '#title' => 'Deadline ',
  '#type' => 'date_select',
  '#date_title' => 'Deadline',
  '#date_format' => 'm/Y',
  '#date_year_range' => '0:+1',
  '#default_value' => variable_get('deadline', date('Y-m')),
  '#description' => t('Deadline description'),

If you use this instead of the form api date type element, then the value is stored differently. The form api date element is like this:

  'year' => 2013, 
  'month' => 12,
  'day' => 31,

Whereas the date_select form element is stored as a string:


Although for the example above the value is formatted like this:


So you'll need to adjust the areas of your site that use this data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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