Skip to main content
Login

Sign in to access your account

Time Picker - National Design System

A time field with lists of hours, minutes and seconds in a panel under it

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

Options
Structure
Format
Minute step
Bounds
Size
Style
State
More
Preview
<div class="nds-form-container nds-time-picker"> <div class="nds-form-header"> <label for="time-picker-visit"> <span class="nds-label">Visit time</span> </label> </div> <div class="nds-form-control"> <div class="nds-form-action"> <button type="button" class="nds-btn nds-subtle nds-md nds-time-picker-toggle nds-icon-only" aria-label="Pick a time" data-i18n-aria-label="time-picker.toggle"> <i class="nds-icon nds-hgi-clock-01" aria-hidden="true"></i> </button> </div> <input type="text" id="time-picker-visit" class="nds-input nds-time-input" placeholder="––:––"> <input type="hidden" class="nds-time-value" name="visit-time"> </div> </div>

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:

  • HH and H: the hour from 0 to 23, with and without a leading zero
  • hh and h: the hour from 1 to 12, with and without a leading zero
  • mm and ss: the minute and the second
  • A and a: 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-input and .nds-time-fields starts 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, 9 becomes 09:, and 2: becomes 02:. A digit that makes the hour impossible is ignored: after 2, a 5 is not added. a or p, ص or م, adds AM or PM. A pasted time is read at once in any common form: 2:30 PM shows as 14:30 in 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:3 and a 0 before the 3 make 10: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-value holds the time as HH:mm, or HH:mm:ss when 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 A on public forms, and HH:mm in tools for staff.
  • Write A or a with hh or h. Without AM/PM, the picker reads every time as AM.
  • Add seconds only when the task needs them.
  • Set data-step to 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 name on .nds-time-value only. The text box holds display text, such as 02:30 م, and does not post.
  • Read the time from .nds-time-value, or with NDS.TimePicker.getValue().
  • To show a saved time, write it in the value of .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.

TokenPreviewValue
--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
var input = document.querySelector('#time-picker-visit'); // Log the 24-hour time at each change input.addEventListener('change', function () { console.log(NDS.TimePicker.getValue(input)); }); // Show a saved time NDS.TimePicker.setValue(input, '14:30');

The full API is in the banner of _js/nds-time-picker.js.

Last Modified Date: 05/11/2026 - 12:02 PM
Was this page useful?
60% of users said Yes from 2843 Feedbacks