Components

Date input

Use the date input component to help users enter a memorable date.

<div class="govuk-form-group">
  <fieldset class="govuk-fieldset" aria-describedby="dob-hint" role="group">
    <legend class="govuk-fieldset__legend govuk-fieldset__legend--xl">
      <h1 class="govuk-fieldset__heading">
        What is your date of birth?
      </h1>
    </legend>
    <span id="dob-hint" class="govuk-hint">
      For example, 31 3 1980
    </span>
    <div class="govuk-date-input" id="dob">
      <div class="govuk-date-input__item">
        <div class="govuk-form-group">
          <label class="govuk-label govuk-date-input__label" for="dob-day">
            Day
          </label>
          <input class="govuk-input govuk-date-input__input govuk-input--width-2" id="dob-day" name="dob-day" type="number" pattern="[0-9]*">
        </div>
      </div>
      <div class="govuk-date-input__item">
        <div class="govuk-form-group">
          <label class="govuk-label govuk-date-input__label" for="dob-month">
            Month
          </label>
          <input class="govuk-input govuk-date-input__input govuk-input--width-2" id="dob-month" name="dob-month" type="number" pattern="[0-9]*">
        </div>
      </div>
      <div class="govuk-date-input__item">
        <div class="govuk-form-group">
          <label class="govuk-label govuk-date-input__label" for="dob-year">
            Year
          </label>
          <input class="govuk-input govuk-date-input__input govuk-input--width-4" id="dob-year" name="dob-year" type="number" pattern="[0-9]*">
        </div>
      </div>
    </div>
  </fieldset>
</div>
Nunjucks macro options

Use options to customise the appearance, content and behaviour of a component when using a macro, for example, changing the text.

Some options are required for the macro to work; these are marked as "Required" in the option description.

If you're using Nunjucks macros in production with "html" options, or ones ending with "html", you must sanitise the HTML to protect against cross-site scripting exploits.

Primary options
Name Type Description
id string This is used for the main component and to compose id attribute for each item.
namePrefix string Optional prefix. This is used to prefix each item.name using -.
items array Required. An array of input objects with name, value and classes. See items.
hint object Options for the hint component. See hint.
errorMessage object Options for the error message. See errorMessage.
formGroup object Options for the form-group wrapper See formGroup.
fieldset object Options for the fieldset component (e.g. legend). See fieldset.
classes string Classes to add to the date-input container.
attributes object HTML attributes (for example data attributes) to add to the date-input container.
Options for items
Name Type Description
id string Item-specific id. If provided, it will be used instead of the generated id.
name string Required. Item-specific name attribute.
label string Required. Item-specific label text. If provided, this will be used instead of name for item label text.
value string If provided, it will be used as the initial value of the input.
classes string Classes to add to date input item.
Options for formGroup
Name Type Description
classes string Optional classes to add to the form group (e.g. to show error state for the whole group)
Options for hint
Name Type Description
text string Required. If `html` is set, this is not required. Text to use within the hint. If `html` is provided, the `text` argument will be ignored.
html string Required. If `text` is set, this is not required. HTML to use within the hint. If `html` is provided, the `text` argument will be ignored.
id string Required. Optional id attribute to add to the hint span tag.
classes string Classes to add to the hint span tag.
attributes object HTML attributes (for example data attributes) to add to the hint span tag.
{% from "date-input/macro.njk" import govukDateInput %}

{{ govukDateInput({
  id: "dob",
  namePrefix: "dob",
  fieldset: {
    legend: {
      text: "What is your date of birth?",
      isPageHeading: true,
      classes: "govuk-fieldset__legend--xl"
    }
  },
  hint: {
    text: "For example, 31 3 1980"
  }
}) }}

When to use this component

Use the date input component when you’re asking users for a date they’ll already know, or can look up without using a calendar.

When not to use this component

Do not use the date input component if users are unlikely to know the exact date of the event you’re asking about.

Read more about how to ask users for dates.

How it works

The date input component consists of 3 fields to let users enter a day, month and year.

The 3 date fields are grouped together in a <fieldset> with a <legend> that describes them, as shown in the examples on this page. This is usually a question, like ‘What is your date of birth?’.

If you’re asking one question per page, you can set the contents of the <legend> as the page heading. This is good practice as it means that users of screen readers will only hear the contents once.

Read more about why and how to set legends as headings.

There are 2 ways to use the date input component. You can use HTML or, if you’re using Nunjucks or the GOV.UK Prototype Kit, you can use the Nunjucks macro.

<div class="govuk-form-group">
  <fieldset class="govuk-fieldset" aria-describedby="dob-hint" role="group">
    <legend class="govuk-fieldset__legend govuk-fieldset__legend--xl">
      <h1 class="govuk-fieldset__heading">
        What is your date of birth?
      </h1>
    </legend>
    <span id="dob-hint" class="govuk-hint">
      For example, 31 3 1980
    </span>
    <div class="govuk-date-input" id="dob">
      <div class="govuk-date-input__item">
        <div class="govuk-form-group">
          <label class="govuk-label govuk-date-input__label" for="dob-day">
            Day
          </label>
          <input class="govuk-input govuk-date-input__input govuk-input--width-2" id="dob-day" name="dob-day" type="number" pattern="[0-9]*">
        </div>
      </div>
      <div class="govuk-date-input__item">
        <div class="govuk-form-group">
          <label class="govuk-label govuk-date-input__label" for="dob-month">
            Month
          </label>
          <input class="govuk-input govuk-date-input__input govuk-input--width-2" id="dob-month" name="dob-month" type="number" pattern="[0-9]*">
        </div>
      </div>
      <div class="govuk-date-input__item">
        <div class="govuk-form-group">
          <label class="govuk-label govuk-date-input__label" for="dob-year">
            Year
          </label>
          <input class="govuk-input govuk-date-input__input govuk-input--width-4" id="dob-year" name="dob-year" type="number" pattern="[0-9]*">
        </div>
      </div>
    </div>
  </fieldset>
</div>
Nunjucks macro options

Use options to customise the appearance, content and behaviour of a component when using a macro, for example, changing the text.

Some options are required for the macro to work; these are marked as "Required" in the option description.

If you're using Nunjucks macros in production with "html" options, or ones ending with "html", you must sanitise the HTML to protect against cross-site scripting exploits.

Primary options
Name Type Description
id string This is used for the main component and to compose id attribute for each item.
namePrefix string Optional prefix. This is used to prefix each item.name using -.
items array Required. An array of input objects with name, value and classes. See items.
hint object Options for the hint component. See hint.
errorMessage object Options for the error message. See errorMessage.
formGroup object Options for the form-group wrapper See formGroup.
fieldset object Options for the fieldset component (e.g. legend). See fieldset.
classes string Classes to add to the date-input container.
attributes object HTML attributes (for example data attributes) to add to the date-input container.
Options for items
Name Type Description
id string Item-specific id. If provided, it will be used instead of the generated id.
name string Required. Item-specific name attribute.
label string Required. Item-specific label text. If provided, this will be used instead of name for item label text.
value string If provided, it will be used as the initial value of the input.
classes string Classes to add to date input item.
Options for formGroup
Name Type Description
classes string Optional classes to add to the form group (e.g. to show error state for the whole group)
Options for hint
Name Type Description
text string Required. If `html` is set, this is not required. Text to use within the hint. If `html` is provided, the `text` argument will be ignored.
html string Required. If `text` is set, this is not required. HTML to use within the hint. If `html` is provided, the `text` argument will be ignored.
id string Required. Optional id attribute to add to the hint span tag.
classes string Classes to add to the hint span tag.
attributes object HTML attributes (for example data attributes) to add to the hint span tag.
{% from "date-input/macro.njk" import govukDateInput %}

{{ govukDateInput({
  id: "dob",
  namePrefix: "dob",
  fieldset: {
    legend: {
      text: "What is your date of birth?",
      isPageHeading: true,
      classes: "govuk-fieldset__legend--xl"
    }
  },
  hint: {
    text: "For example, 31 3 1980"
  }
}) }}

Never automatically tab users between the fields of the Date input because this can be confusing and may clash with normal keyboard controls.

Error messages

Error messages should be styled like this:

<div class="govuk-form-group govuk-form-group--error">
  <fieldset class="govuk-fieldset" aria-describedby="dob-hint dob-error" role="group">
    <legend class="govuk-fieldset__legend govuk-fieldset__legend--xl">
      <h1 class="govuk-fieldset__heading">
        What is your date of birth?
      </h1>
    </legend>
    <span id="dob-hint" class="govuk-hint">
      For example, 31 3 1980
    </span>
    <span id="dob-error" class="govuk-error-message">
      Date of birth must be in the past
    </span>
    <div class="govuk-date-input" id="dob">
      <div class="govuk-date-input__item">
        <div class="govuk-form-group">
          <label class="govuk-label govuk-date-input__label" for="dob-day">
            Day
          </label>
          <input class="govuk-input govuk-date-input__input govuk-input--width-2 govuk-input--error" id="dob-day" name="dob-day" type="number" value="6" pattern="[0-9]*">
        </div>
      </div>
      <div class="govuk-date-input__item">
        <div class="govuk-form-group">
          <label class="govuk-label govuk-date-input__label" for="dob-month">
            Month
          </label>
          <input class="govuk-input govuk-date-input__input govuk-input--width-2 govuk-input--error" id="dob-month" name="dob-month" type="number" value="3" pattern="[0-9]*">
        </div>
      </div>
      <div class="govuk-date-input__item">
        <div class="govuk-form-group">
          <label class="govuk-label govuk-date-input__label" for="dob-year">
            Year
          </label>
          <input class="govuk-input govuk-date-input__input govuk-input--width-4 govuk-input--error" id="dob-year" name="dob-year" type="number" value="2076" pattern="[0-9]*">
        </div>
      </div>
    </div>
  </fieldset>
</div>
Nunjucks macro options

Use options to customise the appearance, content and behaviour of a component when using a macro, for example, changing the text.

Some options are required for the macro to work; these are marked as "Required" in the option description.

If you're using Nunjucks macros in production with "html" options, or ones ending with "html", you must sanitise the HTML to protect against cross-site scripting exploits.

Primary options
Name Type Description
id string This is used for the main component and to compose id attribute for each item.
namePrefix string Optional prefix. This is used to prefix each item.name using -.
items array Required. An array of input objects with name, value and classes. See items.
hint object Options for the hint component. See hint.
errorMessage object Options for the error message. See errorMessage.
formGroup object Options for the form-group wrapper See formGroup.
fieldset object Options for the fieldset component (e.g. legend). See fieldset.
classes string Classes to add to the date-input container.
attributes object HTML attributes (for example data attributes) to add to the date-input container.
Options for items
Name Type Description
id string Item-specific id. If provided, it will be used instead of the generated id.
name string Required. Item-specific name attribute.
label string Required. Item-specific label text. If provided, this will be used instead of name for item label text.
value string If provided, it will be used as the initial value of the input.
classes string Classes to add to date input item.
Options for formGroup
Name Type Description
classes string Optional classes to add to the form group (e.g. to show error state for the whole group)
Options for hint
Name Type Description
text string Required. If `html` is set, this is not required. Text to use within the hint. If `html` is provided, the `text` argument will be ignored.
html string Required. If `text` is set, this is not required. HTML to use within the hint. If `html` is provided, the `text` argument will be ignored.
id string Required. Optional id attribute to add to the hint span tag.
classes string Classes to add to the hint span tag.
attributes object HTML attributes (for example data attributes) to add to the hint span tag.
{% from "date-input/macro.njk" import govukDateInput %}

{{ govukDateInput({
  id: "dob",
  namePrefix: "dob",
  fieldset: {
    legend: {
      text: "What is your date of birth?",
      isPageHeading: true,
      classes: "govuk-fieldset__legend--xl"
    }
  },
  hint: {
    text: "For example, 31 3 1980"
  },
  errorMessage: {
    text: "Date of birth must be in the past"
  },
  items: [
    {
      classes: "govuk-input--width-2 govuk-input--error",
      name: "day",
      value: "6"
    },
    {
      classes: "govuk-input--width-2 govuk-input--error",
      name: "month",
      value: "3"
    },
    {
      classes: "govuk-input--width-4 govuk-input--error",
      name: "year",
      value: "2076"
    }
  ]
}) }}

Make sure errors follow the guidance in error message and have specific error messages for specific error states.

Check the entire date, not the day, month and year separately.

Avoid assuming what is wrong with the date. For example, if a user enters ‘31 2 2016’, the day may be right and the month may be wrong. An error message that mentions February may be confusing if they meant to enter ‘1’.

If nothing is entered

Say ‘Enter [whatever it is]’.
For example, ‘Enter your date of birth’.

If an incomplete date is entered and you know what is missing

Say ‘[whatever it is] must include a [whatever is missing]’.
For example, ‘Date of birth must include a month’ or ‘Date of birth must include a day and month’.

If an incomplete date is entered and you do not know what is missing

Say ‘Enter [whatever it is] and include a day, month and year’.
For example, ‘Enter your date of birth and include a day, month and year’.

If the date entered is not a real one

Say ‘Enter a real [whatever it is]’.
For example, ‘Enter a real date of birth’.

If the date is in the future when it needs to be in the past

Say ‘[whatever it is] must be in the past’.
For example, ‘Date of birth must be in the past’.

If the date is in the future when it needs to be today or in the past

Say ‘[whatever it is] must be today or in the past’.
For example, ‘Date of birth must be today or in the past’.

If the date is in the past when it needs to be in the future

Say ‘[whatever it is] must be in the future’.
For example, ‘The date your course ends must be in the future’.

If the date is in the past when it needs to be today or in the future

Say ‘[whatever it is] must be today or in the future’.
For example, ‘The date your course ends must be today or in the future’.

If the date must be the same as or after another date

Say ‘[whatever it is] must be the same as or after [date and optional description]’.
For example, ‘The date your course ends must be the same as or after 1 September 2017 when you started the course’.

If the date must be after another date

Say ‘[whatever it is] must be after [date and optional description]’.
For example, ‘The day your course ends must be after 1 September 2017’.

If the date must be the same as or before another date

Say ‘[whatever it is] must be the same as or before [date and optional description]’.
For example, ‘The date of Gordon’s last exam must be the same as or before 31 August 2017 when they left school’.

If the date must be before another date

Say ‘[whatever it is] must be before [date and optional description]’.
For example, ‘The date of Gordon’s last exam must be the same as or before 31 August 2017’.

If the date must be between two dates

Say ‘[whatever it is] must be between [date] and [date and optional description]’.
For example, ‘The date your contract started must be between 1 September 2017 and 30 September 2017 when you were self-employed’.

Research on this component

If you’ve used this component, get in touch to share your user research findings.

More research is needed to determine the extent to which users struggle to enter months as numbers, and whether allowing them to enter months as text is helpful.

Get in touch

If you’ve got a question, idea or suggestion share it in #govuk-design-system on cross-government Slack (open in app) or email the Design System team on govuk-design-system-support@digital.cabinet-office.gov.uk

Discuss ‘Date input’ on GitHub