Component

Datetime fields (experimental)

Use the datetime fields component to help users enter a datetime for a specific event.

This differs from the Form date input component in a few notable ways. It builds upon that component by adding time fields. Times are chosen using select elements.

Search for usage of this component on GitHub.

Overview

How it looks (preview) (preview all)

Date
Time

:

How to call this component

<%= render "govuk_publishing_components/components/datetime_fields", {} %>

GOV.UK Design System

This component incorporates components from the GOV.UK Design System:

Accessibility acceptance criteria

Inputs in the component must:

  • accept focus
  • be focusable with a keyboard
  • be usable with a keyboard
  • be usable with touch
  • indicate when they have focus
  • be recognisable as form input elements
  • have correctly associated labels

Child components

This component contains the following components. Note that these components may also contain child components.

Other examples

Standard options

This component uses the component wrapper helper. It accepts the following options and applies them to the parent element of the component. See the component wrapper helper documentation for more detail.

See all standard options
  • id - accepts a string for the element ID attribute
  • data_attributes - accepts a hash of data attributes
  • aria - accepts a hash of aria attributes
  • classes - accepts a space separated string of classes, these should not be used for styling and must be prefixed with js-
  • margin_bottom - accepts a number from 0 to 9 (0px to 60px) using the GOV.UK Frontend spacing scale
  • role - accepts a space separated string of roles
  • lang - accepts a language attribute value
  • open - accepts an open attribute value (true or false)
  • hidden - accepts an empty string, ‘hidden’, or ‘until-found’
  • tabindex - accepts an integer. The integer can also be passed as a string
  • dir - accepts ‘rtl’, ‘ltr’, or ‘auto’
  • type - accepts any valid type attribute e.g. ‘button’, ‘submit’, ‘text’
  • rel - accepts any valid rel attribute e.g. ‘nofollow’
  • target - accepts a valid target attribute e.g. ‘_blank’
  • title - accepts any string
  • draggable - accepts a draggable attribute value (“true” or “false”)

With custom field names and ids (preview)

Date
Time

:

<%= render "govuk_publishing_components/components/datetime_fields", {
  year: {
    name: "year_field_name",
    id: "year_id"
  },
  month: {
    name: "month_field_name",
    id: "month_id"
  },
  day: {
    name: "day_field_name",
    id: "day_id"
  },
  hour: {
    name: "hour_field_name",
    id: "hour_id"
  },
  minute: {
    name: "minute_field_name",
    id: "minute_id"
  }
} %>

With custom heading options (preview)

The date

The time

:

<%= render "govuk_publishing_components/components/datetime_fields", {
  heading_level: 2,
  heading_size: "l",
  date_heading: "The date",
  time_heading: "The time"
} %>

With hint text (preview)

Date
For example, 01 August 2022
Time
For example, 09:30 or 19:30

:

<%= render "govuk_publishing_components/components/datetime_fields", {
  date_hint: "For example, 01 August 2022",
  time_hint: "For example, 09:30 or 19:30"
} %>

With values (preview)

Date
Time

:

<%= render "govuk_publishing_components/components/datetime_fields", {
  year: {
    value: 2022
  },
  month: {
    value: 1
  },
  day: {
    value: 2
  },
  hour: {
    value: 9
  },
  minute: {
    value: 30
  }
} %>

With error items (preview)

Error: Descriptive error 1
Descriptive error 2

Date

Error:

Error:

Error:

Time

:

<%= render "govuk_publishing_components/components/datetime_fields", {
  error_items: [
    {
      text: "Descriptive error 1"
    },
    {
      text: "Descriptive error 2"
    }
  ]
} %>

With separate date time error items (preview)

Date

Error: Descriptive date error 1
Descriptive date error 2

Error:

Error:

Error:

Time

Error: Descriptive time error 1
Descriptive time error 2

:

<%= render "govuk_publishing_components/components/datetime_fields", {
  error_items_date: [
    {
      text: "Descriptive date error 1"
    },
    {
      text: "Descriptive date error 2"
    }
  ],
  error_items_time: [
    {
      text: "Descriptive time error 1"
    },
    {
      text: "Descriptive time error 2"
    }
  ]
} %>