Date picker
About this component
The date picker is a component that helps users to complete date input fields. Users can select a specific date from a calendar. The date picker works with keyboard, mouse or touch screen.
Why we use this component
The date picker is a progressive enhancement to text inputs that lets users choose a date from a calendar interface.
Being able to choose a date from a calendar can be helpful when the day of the week is important to the choice of date.
Date ranges and disabled dates
Allowed date ranges for a date picker can be set by specifying earliest and latest allowed dates. You can disable individual dates.
In this example, a latest date of 28th May 2023 has been set and two days in May 2023 are not available.
These options can be set either with data attributes or in JavaScript. See the implementation section of this page for instructions.
Individual date inputs for day, month and year
By default, a date picker uses a single text input for the date. You can also use separate text fields for each part of the date if you prefer.
Website analytics
To understand user behaviour, you can track clicks on date picker items through the original page path, the click text, and a data attribute showing the element interacted with.
You can track clicks on buttons inside a date picker using the standard Button tracking.
The Design System’s ‘tracking’ script adds these data attributes.
Accessibility
The date picker’s calendar dialog has an ARIA role of ‘grid’ to give semantic meaning to its content. This helps users of assistive technology understand how to interact with the component.
Keyboard support
Users can navigate the calendar by using the cursor keys to move around the calendar, and can use the enter key or spacebar to select a date.
The following table lists the keyboard commands that the date picker supports.
Element | Key | Action |
---|---|---|
Calendar button | Space ,Enter |
Opens the date picker. If there is a current date set in the text input, the date picker has that date focussed. Otherwise, the date picker focuses on today's date. |
Date picker | Tab |
Moves focus to the next element in the tab order. If tabbing away from the last focusable element in the tab order, moves focus to the first focusable element in the date picker. |
Date picker | Shift + Tab |
Moves focus to the previous element in the tab order. If tabbing away from the first focusable element in the tab order, moves focus to the last focusable element in the date picker. |
Month and year buttons | Space ,Enter |
Change the current month or year displayed in the date picker. |
Dates | Space ,Enter |
Selects the focussed date, closes the date picker and moves focus back to the calendar button. Updates the accessible name of the calendar button to show the selected date. |
Dates | Up |
Moves focus to the same day of the previous week, changing the displayed month if necessary. |
Dates | Down |
Moves focus to the same day of the next week, changing the displayed month if necessary. |
Dates | Left |
Moves focus to the previous day, changing the displayed month if necessary. |
Dates | Right |
Moves focus to the next day, changing the displayed month if necessary. |
Dates | Home |
Moves focus to the first day of the current week. |
Dates | End |
Moves focus to the last day of the current week. |
Dates | Page Up |
Shows the previous month and focuses on the same day of the month. |
Dates | Shift + Page Up |
Shows same month in the previous year and focuses on the same day of the month. |
Dates | Page Down |
Shows the next month and focuses on the same day of the month. |
Dates | Shift + Page Down |
Shows same month in the next year and focuses on the same day of the month. |
Cancel button | Space ,Enter |
Closes the date picker and makes no change to the date in the text input. The button to open the calendar has focus. |
OK button | Space ,Enter |
Closes the date picker and updates the date in the text input with the chosen date in the date picker. The button to open the calendar has focus. |
Implementation
The date picker component needs JavaScript enabled in the user’s browser. If a user does not have JavaScript enabled the text field will behave as normal, without the date picker enhancements.
Date formats
The date picker component can use a variety of date formats, with the UK style of date/month/year used by default.
Use an attribute of data-dateformat
on the input element to specify one of the following. The default is DMY.
This is how each format would show the date 14th March 2020:
DMY
-14/03/2020
MDY
-03/14/2020
YMD
-2020/03/14
The values for data-mindate
, data-maxdate
and data-disableddates
all use this date format.
Allowed date ranges
Use an attribute of data-mindate
to specify the earliest possible date for the calendar. Users can not select dates before this date.
Use an attribute of data-maxdate
to specify the latest possible date for the calendar. Users can not select dates before this date.
These attributes should be set on the date picker element. The date format used for both of these is what you specify in data-dateformat
or the default, DMY
.
Disabled dates
Use an attribute of data-disableddates
to specify a list of dates that the user will not be able to select.
The value of this attribute should be a space-separated list of dates. These dates should be in the format you specify in data-dateformat
or the default, DMY
.
JavaScript
Set up a date picker by creating a new DatePicker object. It takes two parameters:
- the DOM element of the date picker (element with class
ds_datepicker
) - an optional object of customisation settings
The customisation settings can have the following properties:
dateSelectCallback
: an optional function called when a user selects a date from the date picker. It expects a ‘date’ parameter, which is a JavaScript date object.disabledDates
: an array of dates to disable in the date pickerimagePath
: the path to the location of your icons sprite. Default is/assets/images/icons/
maxDate
: a JavaScript date object for the latest selectable dateminDate
: a JavaScript date object for the earliest selectable date
Dates that have been set using JavaScript will override values for these dates that you have set using data attributes.
Example JavaScript
const datePicker = new window.DS.components.DSDatePicker(
document.getElementById('my-date-picker'),
{
dateSelectCallback: function (date) {
console.log(`the selected date is ${date}`);
},
disabledDates: [
new Date(2023, 4, 12),
new Date(2023, 4, 13)
],
imagePath: '/my/image/path/',
maxDate: new Date(),
minDate: new Date(2019, 1, 1)
}
);
datePicker.init();
Live example
Create a tenancy agreement (mygov.scot)
Feedback, help and support
If you need help or support you can e-mail us at designsystem@gov.scot
There is a problem
Thanks for your feedback