Overview
A time picker is a field for a time of day. It comes in two structures:
- Field: a text box with a panel under it. The user types a time, or opens the panel with the clock button and picks the hour and the minute from two lists. The format can add a list for the second and a list for AM or PM.
- Boxes: an hour box and a minute box, for a 24-hour time.
Pick another component when:
- the user picks a day: Date Picker
- the user enters a duration, such as 2 hours 30 minutes: number fields in Forms
Markup
Variants
A Format or Seconds choice changes two elements: write data-format on .nds-time-picker and a matching placeholder on .nds-time-input. Seconds adds :ss to the format and :–– to the placeholder: HH:mm:ss and ––:––:–– for 24-hour, hh:mm:ss A and ––:––:–– –– for 12-hour. A Bounds choice writes both data-min-time and data-max-time: on .nds-time-input, or on .nds-time-fields in Boxes. Boxes is 24-hour only, so Format, Seconds, Minute step, Size and Style are off on it. Set another format, step or bound by hand: see the Data Attributes table.
| Group | Option | Markup | On element | Use |
|---|---|---|---|---|
| Structure | Field (default) | — | — | One text box, with a panel of lists that the clock button opens. It takes every format |
| Structure | Boxes (hint: An hour box and a minute box) | canon #time-picker-boxes |
— | An hour box and a minute box, 24-hour only, with no panel. See Boxes |
| Format | 24-hour (default) | — | — | The field shows 14:30. The panel has an Hour and a Minute list |
| Format | 12-hour | [data-format="hh:mm A"] |
.nds-time-picker:not(.nds-time-fields) |
The field shows 02:30 PM, and the panel adds an AM/PM list. See Time Format |
| Format | 12-hour | [placeholder="––:–– ––"] |
.nds-time-input |
|
| Seconds | Seconds | [data-format="HH:mm:ss"] |
.nds-time-picker:not(.nds-time-fields):not([data-format="hh:mm A"]) |
The field shows the second too, such as 14:30:15, and the panel adds a Second list. See Time Format |
| Seconds | Seconds | [placeholder="––:––:––"] |
.nds-time-input:not([placeholder="––:–– ––"]) |
|
| Seconds | Seconds | [data-format="hh:mm:ss A"] |
.nds-time-picker[data-format="hh:mm A"] |
|
| Seconds | Seconds | [placeholder="––:––:–– ––"] |
.nds-time-input[placeholder="––:–– ––"] |
|
| Minute step | 5 minutes (default) | — | — | The Minute list shows 00, 05, 10 and on to 55 |
| Minute step | 15 minutes | [data-step="15"] |
.nds-time-picker:not(.nds-time-fields) |
The Minute list shows 00, 15, 30 and 45. See Minute Step |
| Minute step | 30 minutes | [data-step="30"] |
.nds-time-picker:not(.nds-time-fields) |
The Minute list shows 00 and 30. See Minute Step |
| Bounds | None (default) | — | — | The user can pick any time |
| Bounds | Working hours (hint: Only 09:00 to 17:30 can be picked) | [data-min-time="09:00"] |
.nds-time-input |
The user can pick a time from 09:00 to 17:30 only. See Time Bounds |
| Bounds | Working hours (hint: Only 09:00 to 17:30 can be picked) | [data-max-time="17:30"] |
.nds-time-input |
|
| Bounds | Working hours (hint: Only 09:00 to 17:30 can be picked) | [data-min-time="09:00"] |
.nds-time-fields |
|
| Bounds | Working hours (hint: Only 09:00 to 17:30 can be picked) | [data-max-time="17:30"] |
.nds-time-fields |
|
| Size | LG (default) | — | — | 40px high. It needs no class |
| Size | MD | .nds-md |
.nds-time-picker.nds-form-container |
32px high, with smaller text, for a table filter or a side panel |
| Style | Outline (default) | — | — | A border on the page background |
| Style | Lighter (hint: Light fill and no border) | .nds-lighter |
.nds-time-picker.nds-form-container |
A light fill and no border, for a field on a white card |
| Style | Darker (hint: Darker fill and no border) | .nds-darker |
.nds-time-picker.nds-form-container |
A darker fill and no border, for a field on a gray surface |
| State (any) | Disabled | [data-state~="disabled"] |
.nds-time-picker:not([data-state~="readonly"]) |
The user cannot type or open the panel, and the time does not post. Not with Read-only |
| State (any) | Read-only | [data-state~="readonly"] |
.nds-time-picker:not([data-state~="disabled"]) |
The user sees the time but cannot change it. The time posts. Not with Disabled |
| State (any) | Required (hint: Press Validate with the field empty) | [data-required] |
.nds-time-picker |
The form needs a time. It does not submit while the field is empty |
| Field states | Label, info, feedback, required | — | — | Shared by every form field. See Forms |
Behavior
Panel
The clock button, .nds-time-picker-toggle, opens the panel. A click in the text box only places the cursor, so the user can type. Without the button, a click in the text box opens the panel.
The field fills when every list has a value, and each later pick changes it at once. The panel has no Save button. Escape or a click outside the panel closes it.
Time Format
data-format on .nds-time-picker sets how the field shows a time, and which lists the panel holds. The default is HH:mm. Give the field a placeholder with –– for each part, such as ––:–– –– for hh:mm A. The tokens are:
HHandH: the hour from 0 to 23, with and without a leading zerohhandh: the hour from 1 to 12, with and without a leading zerommandss: the minute and the secondAanda: AM or PM, in capitals or in lowercase
Any other character stays as written. ss adds a Second list, and A or a adds an AM/PM list. With data-format="hh:mm A", the field shows 02:30 PM, and .nds-time-value holds 14:30.
Minute Step
data-step on .nds-time-picker sets the gap between the minutes in the Minute list, from 1 to 60. The default is 5. The Second list always shows every second. A saved time off the step, such as 09:07 with a step of 15, gets its own option in the list.
Boxes
.nds-time-fields splits the time into an hour box and a minute box, with a note above each. It is 24-hour only, with no panel and no seconds. A full hour moves the cursor to the minute box. A pasted time, such as 2:30 PM, fills both boxes. The time reaches .nds-time-value only when it is complete and inside the bounds.
Time Bounds
data-min-time and data-max-time on .nds-time-input, or on .nds-time-fields in Boxes, set the first and the last time the user can pick. Write them in 24-hour form, HH:mm or HH:mm:ss, whatever the format. Options outside the bounds are off. A picked hour turns off the minutes outside the bounds in that hour. When a pick moves another list’s value outside the bounds, that list moves to the nearest allowed option.
Disabled and Read-only
data-state~="disabled" on .nds-time-picker disables the text box, the hidden input and the clock button, so the time does not post. With data-state~="readonly", the user cannot type, and the panel does not open. The time still posts. A readonly attribute on .nds-time-input does the same: Forms copies it to the field. In Boxes, the two states work on both boxes. Arrow Up and Arrow Down do not change a read-only box.
Built-in Features
-
Auto-initialization
Every
.nds-time-inputand.nds-time-fieldsstarts on load. The script builds the panel the first time the user opens it. -
Type or Pick
The user can type the time, in Latin or Arabic digits. The digits fill the format as they are typed: the separator appears on its own,
9becomes09:, and2:becomes02:. A digit that makes the hour impossible is ignored: after2, a5is not added.aorp,صorم, adds AM or PM. A pasted time is read at once in any common form:2:30 PMshows as14:30in a 24-hour field, and a paste that is not a time goes in as if the user typed it. Typing in the middle of a full time replaces the digit after the cursor. After the user deletes part of a time, a key goes in at the cursor, and the time takes its format again when its digits are complete:10:3and a0before the3make10:03. The field never takes more digits or separators than the format holds, or a unit past the clock. An edit in the middle is read when the user leaves the field. -
24-Hour Value
The hidden
.nds-time-valueholds the time asHH:mm, orHH:mm:sswhen the format has seconds. It is the same in every format and page language. -
Native Validation
The picker checks the field at each
change. A time that does not match the format, or falls outside the bounds, shows an error under the field and blocks the submit. In Boxes, the error shows when the user leaves the group, and a time with only one box filled blocks the submit. -
Bilingual Labels
List names, box notes, AM and PM, and error messages show in Arabic or English from the page language. They change when the page language changes.
-
Panel Position
The panel opens below the field, lined up with its start edge, or above it when the space below is too small. In a modal or a scrolling box, it moves to
<body>, so nothing clips it.
Best Practices
- Use a time picker for a time of day, such as an appointment, an opening time or a deadline.
- For a date and a time, use a Date Picker and a Time Picker as two fields. The user can then correct one without the other.
- Use
hh:mm Aon public forms, andHH:mmin tools for staff. - Write
Aorawithhhorh. Without AM/PM, the picker reads every time as AM. - Add seconds only when the task needs them.
- Set
data-stepto the gap between the slots the service offers. A step of 1 lists 60 minutes. - Use Boxes only for a 24-hour time. For AM and PM, use the field.
- Keep the Hour and Minute notes in Boxes. Each note is the label of its box.
- Put the
nameon.nds-time-valueonly. The text box holds display text, such as02:30 م, and does not post. - Read the time from
.nds-time-value, or withNDS.TimePicker.getValue(). - To show a saved time, write it in the
valueof.nds-time-value, in 24-hour form. In Boxes, the script fills both boxes from it. - Give the clock button an
aria-label, such as “Pick a time”. It shows only an icon. - Do not put a time picker inside another dropmenu. The panel is a dropmenu itself.
- For the label, info text, feedback and the required mark, see Forms. They work the same on every field.
- The field colors come from the form tokens: see Forms, Tokens.
API
Other Classes
| Class | Element | Effect |
|---|---|---|
.nds-time-picker-menu |
the panel | The script puts it on the panel, beside .nds-dropmenu-menu. Style the panel with it: it stays on the panel when the panel moves to <body> |
Data Attributes
| Attribute | Element | Effect |
|---|---|---|
data-format |
.nds-time-picker |
How the field shows a time, and which lists the panel holds. The default is HH:mm. Not in Boxes. See Time Format |
data-step |
.nds-time-picker |
The gap between the minutes in the Minute list, from 1 to 60. The default is 5. Not in Boxes. See Minute Step |
data-required |
.nds-time-picker |
The form needs a time. It does not submit while the field is empty |
data-state~="disabled", data-state~="readonly" |
.nds-time-picker |
Set it yourself. See Disabled and Read-only |
data-state~="open" |
.nds-time-picker |
The script sets it when the panel opens, and removes it when the panel closes. No NDS style reads it: it is for your CSS. Boxes has no panel, so it never gets it |
data-min-time, data-max-time |
.nds-time-input |
The first and the last time the user can pick, in 24-hour form. See Time Bounds |
data-min-time, data-max-time |
.nds-time-fields |
The same, in Boxes |
inputmode |
.nds-time-input |
The script sets decimal when the format has no AM/PM, so a phone shows a number pad. Its . key types the separator. Write your own inputmode to keep it |
Keyboard
| Key | Where | Effect |
|---|---|---|
| Enter, Space | clock button | Opens or closes the panel |
| Tab, Shift + Tab | panel | Moves between the lists |
| Enter, Space, arrow keys | a list | Opens the list and moves through its options, as in any select in Forms |
| Escape | an open list | Closes the list. The panel stays open |
| Escape | text box or panel | Closes the panel |
| Arrow Up, Arrow Down | the hour box or the minute box | Adds or takes away one. The hour goes from 23 to 00, and the minute from 59 to 00 |
| Arrow Right | the end of the hour box | Moves to the minute box |
| Arrow Left | the start of the minute box | Moves to the hour box |
| Backspace | an empty minute box | Moves to the hour box |
Tokens
It reads these shared tokens. A change to one also changes other components: see Tokens.
Source: the var() reads in _sass/components/_time-picker.scss.
| Token | Preview | Value |
|---|---|---|
--form-field-border-error | var(--border-error)Dark mode var(--border-error-light) |
JavaScript
The NDS.TimePicker script loads after the page shows. Until it loads, each call loads it and returns a promise of the result, so await a call whose result you use.
el can be the .nds-time-input, its .nds-form-control or its .nds-form-container. In Boxes, it can be the .nds-time-fields or either box.
| Method | Effect |
|---|---|
NDS.TimePicker.init() |
Starts every .nds-time-input and .nds-time-fields that has not started. The loader calls it on load. Call it again after you add a field to the page |
NDS.TimePicker.create(input, formControl) |
Starts one field, and returns its instance. formControl is optional: the default is the input’s .nds-form-control. On a field that has started, it returns the same instance. It returns null when the input has no .nds-form-control or no .nds-form-container. In Boxes, pass the .nds-time-fields or a box: formControl is not used, and it returns null when a box is missing |
NDS.TimePicker.getValue(el) |
Returns the time in 24-hour form, such as '14:30', with seconds when the format has them. It returns '' until every list has a value. In Boxes, it returns '' until both boxes hold a time inside the bounds |
NDS.TimePicker.setValue(el, '14:30') |
Writes a 24-hour time, and returns true. It returns false, and changes nothing, when it cannot read the time or the time is outside the bounds |
NDS.TimePicker.clear(el) |
Empties the field and every list, and returns true |
NDS.TimePicker.destroy(el) |
Runs instance.destroy() on the picker of el: the input, or in Boxes the group or a box. NDS.init.destroy() calls it |
NDS.TimePicker.validate(el, options) |
Checks the boxes of a Boxes field and returns { valid, message }. Pass { showMessage: false } to check without a message |
instance.elements |
{ input, formControl, container, … }. Read-only |
instance.isPanelCreated |
true once the panel is built, on the first open |
instance.destroy() |
Removes the panel and its listeners. create() can start the field again. Call it before you remove the field from the page. The instance is on the input as input.ndsTimePicker, and in Boxes on the group as group.ndsTimePicker |
| Event | Fired on | Detail |
|---|---|---|
input, change |
.nds-time-input and .nds-time-value, and they bubble |
Native events, with no detail. Each input fires them when a pick, a typed time, setValue() or clear() changes its value. Read the time with getValue(). In Boxes, .nds-time-value fires them each time the whole time changes or empties |
The full API is in the banner of _js/nds-time-picker.js.