Components

Date input

Use the date input component to help users enter a memorable date or one they can easily look up.

When was your passport issued?

For example, 12 11 2007
<div id="passport-issued" class="govuk-form-group">
  <fieldset
    aria-describedby="passport-issued-hint"
    class="govuk-fieldset"
  >
    <legend class="govuk-fieldset__legend">
      <h1 class="govuk-heading-l">
        When was your passport issued?
      </h1>
    </legend>
    <div id="passport-issued-hint" class="govuk-hint">
      For example, 12 11 2007
    </div>
    <div class="govuk-date-input">
      <div class="govuk-date-input__item">
        <label
          for="passport-issued-day"
          class="govuk-label"
        >
          Day
        </label>
        <input
          type="text"
          id="passport-issued-day"
          name="passport-issued[day]"
          inputmode="numeric"
          class="govuk-input govuk-input--width-2 govuk-date-input__input"
        />
      </div>
      <div class="govuk-date-input__item">
        <label
          for="passport-issued-month"
          class="govuk-label"
        >
          Month
        </label>
        <input
          type="text"
          id="passport-issued-month"
          name="passport-issued[month]"
          inputmode="numeric"
          class="govuk-input govuk-input--width-2 govuk-date-input__input"
        />
      </div>
      <div class="govuk-date-input__item">
        <label
          for="passport-issued-year"
          class="govuk-label"
        >
          Year
        </label>
        <input
          type="text"
          id="passport-issued-year"
          name="passport-issued[year]"
          inputmode="numeric"
          class="govuk-input govuk-input--width-4 govuk-date-input__input"
        />
      </div>
    </div>
  </fieldset>
</div>
<DateInput
  label={
    <h1 className="govuk-heading-l">
      When was your passport issued?
    </h1>
  }
  name="passport-issued"
  hint="For example, 12 11 2007"
/>
Props
Name Type Default Description
toString function Returns a string representation of a string. Returns a string representation of an object. Returns a string representation of an array.
charAt function Returns the character at the specified index.
charCodeAt function Returns the Unicode value of the character at the specified location.
concat function Returns a string that contains the concatenation of two or more strings. Combines two or more arrays. This method returns a new array without modifying any existing arrays.
indexOf function Returns the position of the first occurrence of a substring. Returns the index of the first occurrence of a value in an array, or -1 if it is not present.
lastIndexOf function Returns the last occurrence of a substring in the string. Returns the index of the last occurrence of a specified value in an array, or -1 if it is not present.
localeCompare other Determines whether two strings are equivalent in the current locale. Determines whether two strings are equivalent in the current or specified locale.
match other Matches a string with a regular expression, and returns an array containing the results of that search. Matches a string or an object that supports being matched against, and returns an array containing the results of that search, or null if no matches are found.
replace other Replaces text in a string, using a regular expression or search string. Replaces first match with string or all matches with RegExp. Replaces text in a string, using an object that supports replacement within a string.
search other Finds the first substring match in a regular expression search.
slice function Returns a section of a string. Returns a copy of a section of an array. For both start and end, a negative index can be used to indicate an offset from the end of the array. For example, -2 refers to the second to last element of the array.
split other Split a string into substrings using the specified separator and return them as an array.
substring function Returns the substring at the specified location within a String object.
toLowerCase function Converts all the alphabetic characters in a string to lowercase.
toLocaleLowerCase function Converts all alphabetic characters to lowercase, taking into account the host environment's current locale.
toUpperCase function Converts all the alphabetic characters in a string to uppercase.
toLocaleUpperCase function Returns a string where all alphabetic characters have been converted to uppercase, taking into account the host environment's current locale.
trim function Removes the leading and trailing white space and line terminator characters from a string.
length number Returns the length of a String object. Gets or sets the length of the array. This is a number one higher than the highest index in the array.
substr function Gets a substring beginning at the specified location and having the specified length.
valueOf function Returns the primitive value of the specified object.
codePointAt function Returns a nonnegative integer Number less than 1114112 (0x110000) that is the code point value of the UTF-16 encoded code point starting at the string element at position pos in the String resulting from converting this object to a String. If there is no element at that position, the result is undefined. If a valid UTF-16 surrogate pair does not begin at pos, the result is the code unit at pos.
includes function Returns true if searchString appears as a substring of the result of converting this object to a String, at one or more positions that are greater than or equal to position; otherwise, returns false. Determines whether an array includes a certain element, returning true or false as appropriate.
endsWith function Returns true if the sequence of elements of searchString converted to a String is the same as the corresponding elements of this object (converted to a String) starting at endPosition – length(this). Otherwise returns false.
normalize other Returns the String value result of normalizing the string into the normalization form named by form as specified in Unicode Standard Annex #15, Unicode Normalization Forms.
repeat function Returns a String value that is made from count copies appended together. If count is 0, the empty string is returned.
startsWith function Returns true if the sequence of elements of searchString converted to a String is the same as the corresponding elements of this object (converted to a String) starting at position. Otherwise returns false.
anchor function Returns an `<a>` HTML anchor element and sets the name attribute to the text value
big function Returns a `<big>` HTML element
blink function Returns a `<blink>` HTML element
bold function Returns a `<b>` HTML element
fixed function Returns a `<tt>` HTML element
fontcolor function Returns a `<font>` HTML element and sets the color attribute value
fontsize other Returns a `<font>` HTML element and sets the size attribute value
italics function Returns an `<i>` HTML element
link function Returns an `<a>` HTML element and sets the href attribute value
small function Returns a `<small>` HTML element
strike function Returns a `<strike>` HTML element
sub function Returns a `<sub>` HTML element
sup function Returns a `<sup>` HTML element
padStart function Pads the current string with a given string (possibly repeated) so that the resulting string reaches a given length. The padding is applied from the start (left) of the current string.
padEnd function Pads the current string with a given string (possibly repeated) so that the resulting string reaches a given length. The padding is applied from the end (right) of the current string.
trimEnd function Removes the trailing white space and line terminator characters from a string.
trimStart function Removes the leading white space and line terminator characters from a string.
trimLeft function Removes the leading white space and line terminator characters from a string.
trimRight function Removes the trailing white space and line terminator characters from a string.
matchAll function Matches a string with a regular expression, and returns an iterable of matches containing the results of that search.
replaceAll other Replace all instances of a substring in a string, using a regular expression or search string.
at function Returns a new String consisting of the single UTF-16 code unit located at the specified index. Returns the item located at the specified index.
__@iterator@51 function Iterator
toFixed function Returns a string representing a number in fixed-point notation.
toExponential function Returns a string containing a number represented in exponential notation.
toPrecision function Returns a string containing a number represented either in exponential or fixed-point notation with a specified number of digits.
toLocaleString function Returns a date converted to a string using the current locale. Converts a number to a string by using the current or specified locale. Returns a string representation of an array. The elements are converted to string using their toLocaleString methods.
type other
props other
key other
pop function Removes the last element from an array and returns it. If the array is empty, undefined is returned and the array is not modified.
push function Appends new elements to the end of an array, and returns the new length of the array.
join function Adds all the elements of an array into a string, separated by the specified separator string.
reverse function Reverses the elements in an array in place. This method mutates the array and returns a reference to the same array.
shift function Removes the first element from an array and returns it. If the array is empty, undefined is returned and the array is not modified.
sort function Sorts an array in place. This method mutates the array and returns a reference to the same array.
splice other Removes elements from an array and, if necessary, inserts new elements in their place, returning the deleted elements.
unshift function Inserts new elements at the start of an array, and returns the new length of the array.
every other Determines whether all the members of an array satisfy the specified test.
some function Determines whether the specified callback function returns true for any element of an array.
forEach function Performs the specified action for each element in an array.
map other Calls a defined callback function on each element of an array, and returns an array that contains the results.
filter other Returns the elements of an array that meet the condition specified in a callback function.
reduce other Calls the specified callback function for all the elements in an array. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.
reduceRight other Calls the specified callback function for all the elements in an array, in descending order. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.
find other Returns the value of the first element in the array where predicate is true, and undefined otherwise.
findIndex function Returns the index of the first element in the array where predicate is true, and -1 otherwise.
fill function Changes all array elements from `start` to `end` index to a static `value` and returns the modified array
copyWithin function Returns the this object after copying a section of the array identified by start and end to the same array starting at position target
entries function Returns an iterable of key, value pairs for every entry in the array
keys function Returns an iterable of keys in the array
values function Returns an iterable of values in the array
flatMap other Calls a defined callback function on each element of an array. Then, flattens the result into a new array. This is identical to a map followed by flat with depth 1.
flat other Returns a new array with all sub-array elements concatenated into it recursively up to the specified depth.
__@unscopables@261 function Returns an object whose properties have the value 'true' when they will be absent when used in a 'with' statement.
day string
month string
year string

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.

When was your passport issued?

For example, 12 11 2007
<div id="passport-issued" class="govuk-form-group">
  <fieldset
    aria-describedby="passport-issued-hint"
    class="govuk-fieldset"
  >
    <legend class="govuk-fieldset__legend">
      <h1 class="govuk-heading-l">
        When was your passport issued?
      </h1>
    </legend>
    <div id="passport-issued-hint" class="govuk-hint">
      For example, 12 11 2007
    </div>
    <div class="govuk-date-input">
      <div class="govuk-date-input__item">
        <label
          for="passport-issued-day"
          class="govuk-label"
        >
          Day
        </label>
        <input
          type="text"
          id="passport-issued-day"
          name="passport-issued[day]"
          inputmode="numeric"
          class="govuk-input govuk-input--width-2 govuk-date-input__input"
        />
      </div>
      <div class="govuk-date-input__item">
        <label
          for="passport-issued-month"
          class="govuk-label"
        >
          Month
        </label>
        <input
          type="text"
          id="passport-issued-month"
          name="passport-issued[month]"
          inputmode="numeric"
          class="govuk-input govuk-input--width-2 govuk-date-input__input"
        />
      </div>
      <div class="govuk-date-input__item">
        <label
          for="passport-issued-year"
          class="govuk-label"
        >
          Year
        </label>
        <input
          type="text"
          id="passport-issued-year"
          name="passport-issued[year]"
          inputmode="numeric"
          class="govuk-input govuk-input--width-4 govuk-date-input__input"
        />
      </div>
    </div>
  </fieldset>
</div>
<DateInput
  label={
    <h1 className="govuk-heading-l">
      When was your passport issued?
    </h1>
  }
  name="passport-issued"
  hint="For example, 12 11 2007"
/>

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

If you’re asking more than one question on the page

If you’re asking more than one question on the page, do not set the contents of the <label> as the page heading. Read more about asking multiple questions on question pages.

When was your passport issued?
For example, 12 11 2007
<div id="passport-issued" class="govuk-form-group">
  <fieldset
    aria-describedby="passport-issued-hint"
    class="govuk-fieldset"
  >
    <legend class="govuk-fieldset__legend">
      When was your passport issued?
    </legend>
    <div id="passport-issued-hint" class="govuk-hint">
      For example, 12 11 2007
    </div>
    <div class="govuk-date-input">
      <div class="govuk-date-input__item">
        <label
          for="passport-issued-day"
          class="govuk-label"
        >
          Day
        </label>
        <input
          type="text"
          id="passport-issued-day"
          name="passport-issued[day]"
          inputmode="numeric"
          class="govuk-input govuk-input--width-2 govuk-date-input__input"
        />
      </div>
      <div class="govuk-date-input__item">
        <label
          for="passport-issued-month"
          class="govuk-label"
        >
          Month
        </label>
        <input
          type="text"
          id="passport-issued-month"
          name="passport-issued[month]"
          inputmode="numeric"
          class="govuk-input govuk-input--width-2 govuk-date-input__input"
        />
      </div>
      <div class="govuk-date-input__item">
        <label
          for="passport-issued-year"
          class="govuk-label"
        >
          Year
        </label>
        <input
          type="text"
          id="passport-issued-year"
          name="passport-issued[year]"
          inputmode="numeric"
          class="govuk-input govuk-input--width-4 govuk-date-input__input"
        />
      </div>
    </div>
  </fieldset>
</div>
<DateInput
  label="When was your passport issued?"
  name="passport-issued"
  hint="For example, 12 11 2007"
/>

Use the autocomplete attribute for a date of birth

Use the autocomplete attribute on the date input component when you’re asking for a date of birth. This lets browsers autofill the information on a user’s behalf if they’ve entered it previously.

To do this, set the autocomplete prop to bday. See how to do this in the code snippet in the following example.

What is your date of birth?

For example, 31 3 1980
<div id="dob" class="govuk-form-group">
  <fieldset
    aria-describedby="dob-hint"
    class="govuk-fieldset"
  >
    <legend class="govuk-fieldset__legend">
      <h1 class="govuk-heading-l">
        What is your date of birth?
      </h1>
    </legend>
    <div id="dob-hint" class="govuk-hint">
      For example, 31 3 1980
    </div>
    <div class="govuk-date-input">
      <div class="govuk-date-input__item">
        <label for="dob-day" class="govuk-label">
          Day
        </label>
        <input
          type="text"
          id="dob-day"
          name="dob[day]"
          inputmode="numeric"
          autocomplete="bday-day"
          class="govuk-input govuk-input--width-2 govuk-date-input__input"
        />
      </div>
      <div class="govuk-date-input__item">
        <label for="dob-month" class="govuk-label">
          Month
        </label>
        <input
          type="text"
          id="dob-month"
          name="dob[month]"
          inputmode="numeric"
          autocomplete="bday-month"
          class="govuk-input govuk-input--width-2 govuk-date-input__input"
        />
      </div>
      <div class="govuk-date-input__item">
        <label for="dob-year" class="govuk-label">
          Year
        </label>
        <input
          type="text"
          id="dob-year"
          name="dob[year]"
          inputmode="numeric"
          autocomplete="bday-year"
          class="govuk-input govuk-input--width-4 govuk-date-input__input"
        />
      </div>
    </div>
  </fieldset>
</div>
<DateInput
  label={
    <h1 className="govuk-heading-l">
      What is your date of birth?
    </h1>
  }
  name="dob"
  hint="For example, 31 3 1980"
  autoComplete="bday"
/>

If you are working in production you’ll need to do this to meet WCAG 2.1 Level AA.

You will not normally need to use the autoComplete attribute in prototypes, as users will not generally be using their own devices.

Error messages

Error messages should be styled like this:

When was your passport issued?

For example, 12 11 2007

Error: The date your passport was issued must be in the past

<div
  id="passport-issued"
  class="govuk-form-group govuk-form-group--error"
>
  <fieldset
    aria-describedby="passport-issued-hint"
    class="govuk-fieldset"
  >
    <legend class="govuk-fieldset__legend">
      <h1 class="govuk-heading-l">
        When was your passport issued?
      </h1>
    </legend>
    <div id="passport-issued-hint" class="govuk-hint">
      For example, 12 11 2007
    </div>
    <p
      id="passport-issued-error"
      class="govuk-error-message"
    >
      <span class="govuk-visually-hidden">
        Error:
      </span>
      The date your passport was issued must be in the
      past
    </p>
    <div class="govuk-date-input">
      <div class="govuk-date-input__item">
        <label
          for="passport-issued-day"
          class="govuk-label"
        >
          Day
        </label>
        <input
          type="text"
          id="passport-issued-day"
          name="passport-issued[day]"
          inputmode="numeric"
          class="govuk-input govuk-input--width-2 govuk-date-input__input"
        />
      </div>
      <div class="govuk-date-input__item">
        <label
          for="passport-issued-month"
          class="govuk-label"
        >
          Month
        </label>
        <input
          type="text"
          id="passport-issued-month"
          name="passport-issued[month]"
          inputmode="numeric"
          class="govuk-input govuk-input--width-2 govuk-date-input__input"
        />
      </div>
      <div class="govuk-date-input__item">
        <label
          for="passport-issued-year"
          class="govuk-label"
        >
          Year
        </label>
        <input
          type="text"
          id="passport-issued-year"
          name="passport-issued[year]"
          inputmode="numeric"
          class="govuk-input govuk-input--width-4 govuk-date-input__input"
        />
      </div>
    </div>
  </fieldset>
</div>
<DateInput
  label={
    <h1 className="govuk-heading-l">
      When was your passport issued?
    </h1>
  }
  name="passport-issued"
  hint="For example, 12 11 2007"
  error="The date your passport was issued must be in the past"
/>

If you’re highlighting just one field - either the day, month or year - only style the field that has an error. The error message must say which field has an error, like this:

When was your passport issued?

For example, 12 11 2007

Error: The date your passport was issued must include a year

<div
  id="passport-issued"
  class="govuk-form-group govuk-form-group--error"
>
  <fieldset
    aria-describedby="passport-issued-hint"
    class="govuk-fieldset"
  >
    <legend class="govuk-fieldset__legend">
      <h1 class="govuk-heading-l">
        When was your passport issued?
      </h1>
    </legend>
    <div id="passport-issued-hint" class="govuk-hint">
      For example, 12 11 2007
    </div>
    <p
      id="passport-issued-error"
      class="govuk-error-message"
    >
      <span class="govuk-visually-hidden">
        Error:
      </span>
      The date your passport was issued must include a
      year
    </p>
    <div class="govuk-date-input">
      <div class="govuk-date-input__item">
        <label
          for="passport-issued-day"
          class="govuk-label"
        >
          Day
        </label>
        <input
          type="text"
          id="passport-issued-day"
          name="passport-issued[day]"
          inputmode="numeric"
          class="govuk-input govuk-input--width-2 govuk-date-input__input"
        />
      </div>
      <div class="govuk-date-input__item">
        <label
          for="passport-issued-month"
          class="govuk-label"
        >
          Month
        </label>
        <input
          type="text"
          id="passport-issued-month"
          name="passport-issued[month]"
          inputmode="numeric"
          class="govuk-input govuk-input--width-2 govuk-date-input__input"
        />
      </div>
      <div class="govuk-date-input__item">
        <label
          for="passport-issued-year"
          class="govuk-label"
        >
          Year
        </label>
        <input
          type="text"
          id="passport-issued-year"
          name="passport-issued[year]"
          inputmode="numeric"
          class="govuk-input govuk-input--width-4 govuk-input--error govuk-date-input__input"
        />
      </div>
    </div>
  </fieldset>
</div>
<DateInput
  label={
    <h1 className="govuk-heading-l">
      When was your passport issued?
    </h1>
  }
  name="passport-issued"
  hint="For example, 12 11 2007"
  error={{
    year: "The date your passport was issued must include a year",
  }}
/>

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

If there’s more than one error, show the highest priority error message. In order of priority, show error messages about:

  • missing or incomplete information
  • information that cannot be correct (for example, the number ‘13’ in the month field)
  • information that fails validation for another reason

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’.