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-timeprop: 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
| Name | Type | Default | Description |
|---|---|---|---|
allowedHours | number[] | undefined | — | Configures which hours are available for selection. |
allowedMinutes | number[] | undefined | — | Configures which minutes are available for selection. |
allowedSeconds | number[] | 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. |
defaultDate | string | undefined | — | Date used by default when the model is empty (in the YYYY/MM/DD format). |
disabled | boolean | null | undefined | null | Disabled state |
format12h | boolean | undefined | — | Shows the time in the 12-hour AM/PM format. |
locale | string | 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. |
mask | string | undefined | 'HH:mm' | Date/time format |
modelValue | string | TDatetimeValueRange | (string | TDatetimeValueRange)[] | null | undefined | null | Value of the model |
nowBtn | boolean | undefined | — | Shows the "Now" button. |
readonly | boolean | null | undefined | null | Read-only state |
theme | string | undefined | — | Component theme |
withSeconds | boolean | undefined | — | Adds seconds to the time selection. |
Events
| Name | Type | Description |
|---|---|---|
update:modelValue | [value: string | undefined, date: IRawDate] | The value changed (v-model). Passes the formatted value and the date object. |
Exposed
| Name | Type | Description |
|---|---|---|
setAm | () => void | Switches the 12-hour format to the first half of the day (AM). |
setHour | (hour: number) => void | Sets the hours. |
setMinute | (minute: number) => void | Sets the minutes. |
setNow | () => void | Sets the current system time. |
setPm | () => void | Switches the 12-hour format to the second half of the day (PM). |
setSecond | (second: number) => void | Sets the seconds. |