• Atom
  • CORE COMPONENT
  • Taxonomic name: A-DATE
  • Added on: v3.0.0 (15/09/17)
  • Updated on: v5.5.0 (23/03/21)

Date

Date fields are a control for recording a date by day, month and year.


Design and usage

  • Not all date formats are the same; use the appropriate date format based on the rules below.
  • Dates, open dates, or dates within a long range (5+ years): Provide three text input fields, with hint text of DD MM YYYY to aid users input; see the form row group page for further details
  • Approximate dates: If an exact date is not required (eg. for 'Year built'), allow users to enter free format dates in a single auto-formatting textbox - see textbox format page for further details
  • Time and days: Date and time should be separate fields; date data entry should follow patterns listed here. Do not allow users to select times (eg. when scheduling a callback) that are unavailable
  • For a single date: provide a single input box with a date picker; date picker must only show a valid date range
  • For a range between two dates: Provide two individual input boxes with a date picker to aid entry; date picker must only show a valid date range. See date range control page for further details

For documentation on date input prior to v4.9.0 see our retired date input documentation. If you are using v4.9.0 or above please move to using this version of the date input as soon as possible.

Structure

  • Use a text box for manual entry which will automatically format the date as the user types. The placeholder for this field should be the format the user is required to supply the date in eg. DD/MM/YYYY.
  • Single date picker trigger to aid users in their date selection. Replays date selection if what has been entered is valid.
  • Datepicker:

    • Single month view.
    • Previous/Next chevrons allowing single month navigation.
    • Month and year dropdowns allow for quick navigation to any month or year within the allowed date range.
    • Date buttons for selecting dates.
    • Today's date, indicated on relevant date button via notification blue marker.

Errors

  • Displayed above date input field ie. 'Please select a date'.

Accessibility and screen readers

  • Users can choose to enter the date manually or by using the datepicker.
  • Aria-invalid must be used to indicate when an error has occurred with this element
  • Aria-describedby must be used to link any help, instructional and/or error text with the element/form fields

Non-JS requirements and considerations

  • No datepicker or auto formatting of date entry made available.

Use cases and exception scenarios

  • Date is saved on date selection.
  • Date picker can be closed by navigating away from the date picker. Note: Date will not be saved in this instance.

Examples

Standard date

Interactive example

Code example

<div class="m-form-row">
	<label class="a-label" for="...">...</label>
	<div class="m-form-row__content">
		<p class="m-form-row__instruction" id="...">...</p>
		<input class="a-date" type="text" name="..." pattern="..." id="..." value="..." data-module="a-date" data-max="..." data-min="..." aria-describedby="..." />
		...
	</div>
</div>

Valid date

Interactive example

Code example

<div class="m-form-row is-valid">
	<label class="a-label" for="...">...</label>
	<div class="m-form-row__content">
		<p class="m-form-row__instruction" id="...">...</p>
		<p class="m-form-row__validation-message" id="...">...</p>
		<input class="a-date" type="text" name="..." pattern="..." id="..." value="..." data-module="a-date" data-max="..." data-min="..." aria-describedby="..." />
		...
	</div>
</div>

Errored date

Interactive example

Code example

<div class="m-form-row is-error">
	<label class="a-label" for="...">...</label>
	<div class="m-form-row__content">
		<p class="m-form-row__instruction" id="...">...</p>
		<p class="m-form-row__error-message" id="..."><span class="u-hidden--visually">Error: </span>...</p>
		<input class="a-date" type="text" name="..." pattern="..." id="..." value="..." data-module="a-date" data-max="..." data-min="..." aria-invalid="true" aria-describedby="..." />
		...
	</div>
</div>

Disabled date

Interactive example

Code example

<div class="m-form-row is-disabled">
	<label class="a-label" for="...">...</label>
	<div class="m-form-row__content">
		<p class="m-form-row__instruction" id="...">...</p>
		<input class="a-date" type="text" name="..." pattern="..." id="..." value="..." data-module="a-date" data-max="..." data-min="..." aria-describedby="..." disabled />
		...
	</div>
	</div>

Required date

<div class="m-form-row">
	<label class="a-label" for="...">...</label>
	<div class="m-form-row__content">
		<p class="m-form-row__instruction">...</p>
		<input class="a-date" type="text" name="..." pattern="..." id="..." value="..." data-module="a-date" data-max="..." data-min="..." aria-required="true" />
		...
	</div>
</div>

Development and test

Notes for developers

The inputs must be of type text ensuring a consistent experience for all users. The pattern attribute must be set on the text input eg. for DD/MM/YYYY the attibute should have a value of \d{2}\/\d{2}\/\d{4}.

The input must have the attributes data-min and data-max, in the format YYYY-MM-DD, set to limit the date pickers to a date range. These values should always be set server side, if they are not present the min date will default to 1901-01-01 and the max date will default to today's date.

When disabling form elements, do not solely rely on the is-disabled class, but ensure that the disabled attribute is also set on the relevant form element or input. If an entire set of form elements is to be disabled, the is-disabled class and disabled attribute should be added directly to the fieldset instead, and not to each individual element within it. However if only one element from the set is disabled, then the class and attribute should be applied to that element only.

Any validation (including server-side) on the input field must validate against the entry format that has been specified eg. DD/MM/YYYY. If the native form validation needs to be disabled set the novalidate attribute on the form.

Aria usage for errors and help text

To aid screen reader users in completing forms without error and also with understanding and fixing any errors that have occured, some additional aria attributes are now recommended to be applied across all form fields/rows.

All form row help, explanatory text, form row instructions and form row errors must have a unique id assigned to them and be appropriately linked to either the individual form input or the form row using aria-describedby. This ensures that the help text is read out immediately on focus of the form input/fieldset. When there is more that one item that requires referencing within the aria-describedby attribute the id values must be supplied in the form of a space separated list. For example if there is an error with id of 'err1' and help text with id of 'help1' the value for the aria-describedby attribute would be 'err1 help1'.

All inline form error messages should start with the hidden text 'Error: ' so that when read by out by a screen reader it is immediately clear that the text being read is an error message.

All erroring form fields (or fieldset if a group or radios/checkboxes) must have aria-invalid="true" set on them, ensuring that when a user focus' on an errored form field, the field will be announced as invalid, so the user is then aware that there in an issue that requires fixing.

When linking help or errors, or even adding aria-invalid to the markup careful consideration must be taken as to where it is most appropriate to apply the value. The following rules should be used to decide where the attribute(s) should be applied.

  • Single form field - All attributes should be applied directly to that form field
  • Group of form fields but only one in error or help text applies to only one - Apply the attribute(s) to the only field in error or where the help text applies
  • Group of form field where all in error or the help text applies to all - Apply the attribute(s) to the groups surrounding fieldset (form row)

There may situations where more than one of these rules applies to the overall form row for example a date range where the instruction text applies to both fields so should be linked to the fieldset for the date range but one of the two fields is in error so the error should be linked to just that field not to the fieldset.

Important messages

To help those using assistive technologies, make sure that success/error messages which need to be announced to users have their aria attributes updated to role="alert" and/or aria-live="assertive".

Updating configuration post load

When using the date input, the value, minimum and maximum date settings can be updated programatically after the initial load. To do this, the function setSettings should be called from the a-date input with a JavaScript object containing settings in the same format as below being passed into the function:

Attribute Type Default Description
value String null Date must be in the format 'YYYY-MM-DD'
minDate String null Update the minimum date post load. Date must be in the format 'YYYY-MM-DD'
maxDate String null Update the maximum date post load. Date must be in the format 'YYYY-MM-DD'

For example:

document.addEventListener( 'frameworkready', function () {
	FRAMEWORK.require([ 'blocks/a-date' ], function ( aDateModule ) {
		var dateEl = document.querySelector( '#dateInput' ),
			dateInstance = aDateModule.initInstance( dateEl );		dateInstance.setSettings({
			minDate: '2016-01-01',
			maxDate: '2018-01-01',
		});
	});
});

See Interacting with Framework components example for further details on how to safely interact with framework components.

Using autocomplete

Users appreciate when websites save them time by automatically filling common fields like names, email addresses and other frequently used fields, plus it helps to reduce potential input errors, especially on virtual keyboards and small devices.

Browsers use many methods to determine which fields they can auto-populate based on previously specified data by the user, and you can give hints to the browser by providing both the name attribute and the autocomplete attribute on each input element.

Further details can be found in the developer notes on the form introduction page.

Changing locale settings

The date format and labels within the datepicker can be updated for a particular locale.

en: {
	aDate : {
		locale : 'en-gb',
		format : 'DD/MM/YYYY',
		previousMonth : 'Previous month',
		nextMonth : 'Next month',
		months : [ 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December' ],
		weekdays : [ 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday' ],
		weekdaysShort : [ 'S', 'M', 'T', 'W', 'T', 'F', 'S' ],
		interactionInstruction : 'Use the arrow keys to pick a date',
		triggerLabelSingle : 'Select date',
		confirmLabelSingle : 'Confirm date',
		triggerLabelRange : 'Select dates',
		confirmLabelRange : 'Confirm dates',
		clearLabel : 'Clear',
		totalSelectedSingular : 'Total days selected: {days} day',
		totalSelectedPlural : 'Total days selected: {days} days',
		yearLabel : 'Year',
		monthLabel : 'Month',
		startDateTitle : 'Set start date',
		endDateTitle : 'Set end date',
	},
},

For more information on this, see our page on how to change locale settings within JavaScript.


Notes for testers

  • In IE11 the date icon will look fuzzy; this is a rendering issue with the browser when a background is on an element that has rounded corners.
  • The form must have validation in place to catch any errors with the dates entered and present an error message back to the user. This message does not have to be shown real-time, but can appear once the user has attempted to submit the form.
  • Ensure that the field has a correctly associated label; this not only provides context to the field for assistive technology users, but also a larger clickable area to select the field. Clicking / pressing on the label should cause the date field to gain focus.
  • Users should be able to copy and paste if necessary.
  • Highlight today's date and the selected date with clear variation between them
  • Users should be able to easily identify and distinguish between selectable and non-selectable dates, today's date, the selected date and weekends.
  • The calendar should have a clear hover state to help users identify the correct date.
  • For some applications, past dates may need to be disabled.
  • Clickable / tappable area of each day should be an appropriate size.

Classes overview

The following table gives you a quick overview of the CSS classes that can be applied.

Class Outcome Required Applied to Comments
.a-date Styling for the date input Yes input  

Keyboard operations

TAB

Tabbing to an input field should make the input clearly visually different so that the focus point on the page is obvious to the user.

Component releases

  • Added on: v3.0.0 (15/09/17)
  • Updated on: v5.5.0 (23/03/21)

Latest update:

  • updated: The disabled dates colour within the datepicker has been updated from #9b9b9b to #b8b8b8, to provide better consistency in the colours used within Framework.
  • updated: Additional aria attributes are now recommended to be applied across all form fields/rows so as to aid screen reader users in completing forms without error and also with understanding and fixing any errors that have occurred.

Full version history

A full history of changes and enhancements detailing all minor and major updates to the component.

View full version history

Want something new in Framework, or to chat about an issue you're having with it?

Contact the team