Time picker

Hour and minute, with a starting time

Each part is its own segment. Type digits, or use the arrow keys. The form sends the time as HH:MM under data-name.

An empty field

Without a value the segments show -- and the form sends nothing. The first change fills the other segments with 00.

data-seconds adds seconds

The value becomes HH:MM:SS.

data-hour12 shows 1 to 12 and AM or PM

Only what is shown changes: the value is still 24 hour, so 3:15 PM is sent as 15:15. Press A or P on the last segment.

data-min, data-max and data-step

Minutes move in steps of data-step. A time outside the limits is moved to the nearest one when focus leaves the field.

Date and time in one input group

Put a Date picker and a time picker in one .input-group. The date field uses data-variant="ghost" so the group draws the only box.

Sizes, invalid and disabled

data-size sets the height like an input. aria-invalid="true" draws the red border. disabled dims the field and turns the segments off.

Behavior

.time-picker is an empty element. The script fills it with the segments and a hidden input named by data-name, and keeps that input in the form's format: HH:MM, or HH:MM:SS with data-seconds. It is "" while a segment is empty. You can also write your own <input type="hidden"> inside; its name and value are used. A form reset puts the starting time back.

Arrow keys wrap inside a segment and never carry into the next one. Typed digits are limited to the segment's range, and a segment moves on once it cannot take another digit. Pasting HH:MM into any segment fills the whole field.

API

The script starts every .time-picker on the page, also one added later. On the element, value is read and set ("" or an invalid time clears it).

Methods

MethodDescription
window.tsunami.timePicker.get(el)Returns the value, HH:MM or HH:MM:SS, or "".
window.tsunami.timePicker.set(el, value)Sets the time and sends the events. An empty or invalid value clears it.
window.tsunami.timePicker.mount(el)Starts one element, for one made with a script before the page finished loading.
window.tsunami.timePicker.destroy(el)Removes the segments.

Events

EventOnDescription
tsunami:timechange.time-picker, bubblesSent when the value changes. detail.value is the new value, "" when cleared.
input, changethe hidden input, bubbleSent with it, so a form and the save bar hear the change.

Keyboard

KeyDescription
TabMoves to the next segment, then out of the field.
←, →, :Move to the previous or next segment.
↑, ↓Add or take one, wrapping. Minutes follow data-step. On an empty segment, start from the lowest or the highest.
0–9Type the value. A segment moves on after two digits, or one that cannot take a second.
Home, EndLowest or highest value of the segment.
A, POn the AM or PM segment, set AM or PM.
Backspace, DeleteEmpty the segment.

Accessibility

The field is a role="group" named by its aria-label (default Time). Each segment is a role="spinbutton" with its own aria-label (Hour, Minute, Second, AM or PM), aria-valuemin, aria-valuemax, aria-valuenow and aria-valuetext (Empty while empty). Give the field an aria-label or a label that says what the time is for.

Attributes

Attribute / elementValuesDescription
.time-pickeran empty divThe field. The script adds the segments.
data-namea nameThe name of the hidden input a form sends.
valueHH:MM or HH:MM:SSThe starting time. Without it the field is empty.
data-secondspresent or absentAdds a seconds segment.
data-hour12present or absentShows 1 to 12 and an AM or PM segment. The value stays 24 hour.
data-min, data-maxHH:MMThe earliest and latest time. A time outside is moved to the nearest limit when focus leaves, or on an arrow key.
data-stepminutes, such as 15How far the arrow keys move the minute segment.
data-sizesm, lgThe height of the field: 28px, 36px (default) or 40px.
aria-invalidtrueDraws the red border.
disabledpresent or absentDims the field and turns the segments off.
aria-labeltextNames the group. Defaults to Time.