Skip to content

CdsTimePicker

Time selection: hours, minutes and — if you want — seconds. The value can be typed on the keyboard or stepped through with the arrows, and v-model receives a string in the given format (HH:mm by default), which is convenient to store and to send to the server.

Storybook GitLab Figma
Test coverage: 97% — good coverage
API in specs
21 / 21 props, events, slots and exposed members with a test
Test cases
39 unit · 2 browser
Known bugs
7 pinned by an it.fails test
Code coverage
97.2% lines · 88.3% branches measured in v4.0.0-alpha.13
A11y (axe)
17 recommendations in 15 stories measured in v4.0.0-alpha.11

To pick a date use CdsDatePicker, and if you need a field with a date and a time at once — the ready CdsDateInput with the with-time prop: it puts this clock under the calendar and brings everything together into one string itself.

Anatomy ​

Every block of the time is a field with arrows: the value can be typed on the keyboard or stepped through one unit at a time. The seconds, the AM/PM switch and the "Now" button are added by the with-seconds, format12h and now-btn props.

Variations ​

Value and format ​

The time arrives in v-model as a string in the mask format — HH:mm by default. As the second argument of the update:model-value event the component gives out the parsed date as an object: the year, the month, the day, the hours, the minutes, the seconds. While no value is chosen, the fields are empty and show the 00 hint.

The mask may include the date as well: then the component changes the time only and carries the date over from the current value — that is how one string is shared between a calendar and a clock. The with-time prop of CdsDateInput is built on the same idea. If the model is empty, the date for the new string is taken from default-date (the YYYY/MM/DD format), otherwise it is today.

Seconds ​

with-seconds adds a third block. The mask is extended automatically: the component appends :ss to HH:mm by itself.

The 12-hour format ​

format12h shows the hours from 1 to 12 and the AM/PM switch. This does not affect the value in the model — the format is set by the mask, so for a string with AM/PM use hh:mm A.

The "Now" button ​

now-btn adds a button under the clock that sets the current time — the user does not have to step through the values with the arrows.

Available time ​

allowed-hours, allowed-minutes and allowed-seconds accept arrays of the allowed values: the arrows step through those only, and a value outside the list is pulled by the component to the nearest allowed one.

When the restriction depends on the already chosen time, pass the allowed-time function instead of the arrays. It is called separately for the hours, the minutes and the seconds: the parts that are not being checked arrive as null.

Localization ​

locale accepts a locale name (ru | en) or your own object of translations; without the prop the locale of the application is taken. The caption of the "Now" button and the default format depend on the locale: in en the time is 12-hour, so the hours are shown from 1 to 12 even without format12h — only the AM/PM switch is added by it.

States ​

disabled turns the clock off and dims it, readonly keeps the usual look but forbids changing the value — both with the arrows and from the keyboard.

Guidelines ​

Scenarios ​

Booking a meeting ​

Meetings are scheduled within working hours and by quarters of an hour — allowed-hours and allowed-minutes leave only such values. The user cannot pick a time that would have to be rejected later.

A field with a time ​

Most often the clock lives not on the page but in a menu next to a text field. There is no ready field for the time alone in the system — assemble the control out of CdsTextInput, a CdsMenu with activator="parent" and the clock: all the props and slots of the field and the menu stay available to you. For the manual input add a mask: we recommend the lightweight (2.5 KB) Maska library. If you need the date together with the time, the field already exists — CdsDateInput with with-time.

A "from — to" interval ​

Two clocks can share one rule: the allowed-time of the end looks at the start and does not let an earlier time be picked. When the start is moved past the end, the demo moves the end too — the rule and the code work in pairs.

Date and time ​

The calendar does not pick the time and the clock does not pick the date — put them next to each other with a shared model and the same DD.MM.YYYY HH:mm mask: every component changes its own part of the string. In a field this pair is already assembled — turn on with-time on CdsDateInput.

API ​

Props

NameTypeDefaultDescription
allowedHoursnumber[] | undefined—Configures which hours are available for selection.
allowedMinutesnumber[] | undefined—Configures which minutes are available for selection.
allowedSecondsnumber[] | undefined—Configures which seconds are available for selection.
allowedTime((hr: number | null, min: number | null, sec: number | null) => boolean) | undefined—Configures which time is available for selection.
defaultDatestring | undefined—Date used by default when the model is empty (in the YYYY/MM/DD format).
disabledboolean | null | undefinednullDisabled state
format12hboolean | undefined—Shows the time in the 12-hour AM/PM format.
localestring | TDateLocale | undefined—Locale for the dates. A locale name ('ru' | 'en') or a TDateLocale object. The current locale from useLocale is taken by default, otherwise ru.
maskstring | undefined'HH:mm'Date/time format
modelValuestring | TDatetimeValueRange | (string | TDatetimeValueRange)[] | null | undefinednullValue of the model
nowBtnboolean | undefined—Shows the "Now" button.
readonlyboolean | null | undefinednullRead-only state
themestring | undefined—Component theme
withSecondsboolean | undefined—Adds seconds to the time selection.

Events

NameTypeDescription
update:modelValue[value: string | undefined, date: IRawDate]The value changed (v-model). Passes the formatted value and the date object.

Exposed

NameTypeDescription
setAm() => voidSwitches the 12-hour format to the first half of the day (AM).
setHour(hour: number) => voidSets the hours.
setMinute(minute: number) => voidSets the minutes.
setNow() => voidSets the current system time.
setPm() => voidSwitches the 12-hour format to the second half of the day (PM).
setSecond(second: number) => voidSets the seconds.