TimePicker

TimePicker chooses a time of day in columns of hours, minutes and seconds. TimeRangePicker chooses a start and an end. The values are native Date objects.

Import
import { TimePicker, TimeRangePicker } from 'svelte-ant-design';
Links
src/lib/TimePicker Report an issue Component README

When to use

  • Use a time picker when a person chooses a time of day: the start of a meeting, opening hours, an alarm.
  • Steps and disabled times narrow the columns to what can really be chosen; a person who knows the time can type it.
  • Use TimeRangePicker for a start and an end that belong together, and DatePicker with showTime when the day matters too.

Examples

Value: nothing

Basic

A press opens the columns; the value is a Date and waits for OK. A time can also be typed.

format="HH:mm": two columns
format="HH": the hour alone
a list: the first writes, each reads (try 7.30)
a function writes; nothing typed is read

Format

format writes and reads the text of the field, and its pattern decides which columns exist.

use12Hours: h:mm:ss a
format="h:mm A"

12 hours

use12Hours shows hours from 1 to 12 and a column of AM and PM.

every 2 hours, 15 minutes and 30 seconds

Steps

hourStep, minuteStep and secondStep set the distance between the numbers of a column.

disabledTime: open from 9:00 to 18:30
hideDisabledOptions: they are not shown

Times that cannot be chosen

disabledTime names hours, minutes and seconds to take out; hideDisabledOptions hides them. With nothing chosen a column starts at its first number that can be chosen; scroll up to see the others.

the value waits for OK; onok says it was confirmed
needConfirm=false: every choice is taken
Value: nothing
showNow=false, defaultOpenValue 09:00

OK and Now

By default the value changes on OK only. needConfirm, showNow and defaultOpenValue change how a choice is made.

changeOnScroll with needConfirm=false: scroll a column
Value: nothing
changeOnScroll: the scrolled time waits for OK

Change on scroll

With changeOnScroll the number a column comes to rest on, after you scroll it, is chosen.

No shift chosen

Range

TimeRangePicker asks for the start and then the end, and calls onchange once, when both are chosen.

order=false: a night shift ends before it starts
allowEmpty: the end may stay empty
disabled={[true, false]}: the start is fixed
disabledTime is told which end it is asked for

The ends of a range

Ends are compared by the time of day. Either end may stay empty or be fixed; without order the end may be earlier than the start.

Quick choices of a range

presets stand beside the columns and give both ends.

disabled
inputReadOnly: chosen in the panel only
allowClear=false
allowClear with its own icon and name
placeholder

Disabled, read-only, clear

inputReadOnly keeps typing and the phone keyboard away. allowClear hides or changes the clear button.

large
medium
small

Sizes

large, medium and small: 40, 32 and 24px, as the other fields.

outlined
filled
borderless
underlined

Variants

outlined, filled, borderless and underlined.

Choose a time during opening hours.
The office is busy at this hour.

Status

status paints an error or a warning; say what is wrong in text next to the field.

prefix
From
suffix replaces the clock
UTC
suffix={null}: no icon

Icons

prefix and suffix of the field.

cellRender: the full quarters are marked; extraFooter as a text
extraFooter as a snippet

Own numbers and a footer

cellRender draws the content of a number, which keeps its name; extraFooter adds content under the columns.

open held by the page
Opened by the field 0 times
placement="topRight"
container: the panel is drawn inside the box

The panel

open holds the panel; placement and container change where it is drawn.

ru, with the halves of the day in words
zh-cn
ru, a range

Language

locale brings the texts and the names of AM and PM. English is built in; Russian and Chinese ship with the library, the same objects as for DatePicker.

Callbacks

onchange and onclear; onfocus and onblur fire once for the field and its panel together.

In a compact block

In a SpaceCompact the field joins the controls beside it.

classNames and styles

Classes and styles

classNames and styles address the parts of the field and of the panel.

API

PropertyDescriptionTypeDefault
valueThe chosen time, on a date. Bind it with bind:value.Date | nullnull
formatHow the time is written; with a list the first writes and each reads. The pattern decides the columns. With use12Hours the default is h:mm:ss a.string | string[] | (date: Date) => string'HH:mm:ss'
use12HoursHours from 1 to 12 and a column of AM and PM.booleanfalse
hourStepEvery n-th hour.number1
minuteStepEvery n-th minute.number1
secondStepEvery n-th second.number1
disabledTimeHours, minutes and seconds that cannot be chosen; the argument is the time chosen or judged, null before one is chosen.(date: Date | null) => DisabledTimes—
hideDisabledOptionsDisabled numbers are not shown.booleanfalse
defaultOpenValueThe time and the date a choice starts from; today at 00:00:00 without it.Date—
needConfirmThe value changes on OK only.booleantrue
showNowThe "Now" button.booleantrue
changeOnScrollThe number a column comes to rest on, after the user scrolled it, is chosen.booleanfalse
cellRenderThe content of a number: { column, value, text, selected, disabled, range }.Snippet<[cell: TimeCell]>—
extraFooterUnder the columns.string | Snippet—
placeholderThe hint of the empty field; by default from the locale.string—
disabledThe field cannot be used.booleanfalse
inputReadOnlyThe field cannot be typed in; a phone shows no keyboard.booleanfalse
allowClearA button that empties the value; an object changes its icon, name and tabindex.boolean | { clearIcon?, label?, tabIndex? }true
sizemedium unless a SpaceCompact says otherwise; middle and default are aliases.'large' | 'medium' | 'middle' | 'small' | 'default'—
variantThe look of the field.'outlined' | 'filled' | 'borderless' | 'underlined''outlined'
statusPaints an error or a warning; error also sets aria-invalid.'error' | 'warning'—
prefixBefore the text.string | Snippet—
suffixReplaces the clock icon; null hides it.string | Snippet | null—
openWhether the panel is shown. Bind it with bind:open.booleanfalse
placementThe side the panel prefers; it flips at the edge of the screen.'bottomLeft' | 'bottomRight' | 'topLeft' | 'topRight''bottomLeft'
containerWhere the panel is put (default: the end of <body>).HTMLElement | (anchor) => HTMLElement—
localeThe texts and the names of AM and PM: import ru from 'svelte-ant-design/locales/ru'.DatePickerLocaleen
onchangeThe user changed the value; the second argument is the text.(value, text: string) => void—
onokA time was confirmed with OK or Enter, with needConfirm.(value: Date) => void—
onopenchangeThe panel opened or closed.(open: boolean) => void—
onclearThe clear button emptied the value.() => void—
onfocusThe focus came to the field or its panel from outside.(event: FocusEvent) => void—
onblurThe focus left both.(event: FocusEvent) => void—
classNamesExtra classes of root, input, prefix, suffix, popup, body, cell and footer.TimePickerClassNames—
stylesInline styles of the same parts.TimePickerStyles—
class / styleClass and style of the field.string—
native attributesThe other attributes go to the <input> (id, name, aria-*, autofocus).HTMLInputAttributes—
Differences from Ant Design: values are native Date objects, not dayjs; TimePicker.RangePicker is TimeRangePicker; the focus alone does not open the panel; the keyboard follows the ARIA combobox and listbox patterns; a range has a "Now" button. Not in this version: milliseconds, a masked format, previewValue, panelRender, footer buttons of your own, presets of a single TimePicker and another date library.

TimeRangePicker

TimeRangePicker takes the props of TimePicker; these differ or are its own.

PropertyDescriptionTypeDefault
valueThe start and the end; an end not chosen is null. Bind it with bind:value.[Date | null, Date | null] | nullnull
placeholderThe hints of the two inputs; by default from the locale.[string, string]—
inputLabelsThe accessible names of the two inputs; by default from the locale.[string, string]—
disabledBoth ends, or each. A disabled end keeps its value and fixes the order.boolean | [boolean, boolean]false
allowEmptyAn end that may stay empty.[boolean, boolean][false, false]
separatorBetween the inputs; an arrow by default.string | Snippet—
orderThe start is not after the end, by the time of day: two ends chosen now swap, an end kept from before is asked again.booleantrue
idThe ids of the two inputs.{ start?: string; end?: string }—
autofocusThe start input takes the focus when the page loads.booleanfalse
disabledTimeTimes that cannot be chosen, for the end asked.(date: Date | null, range: 'start' | 'end') => DisabledTimes—
presetsQuick choices that give both ends.{ label: string | Snippet; value: [Date, Date] | (() => [Date, Date]) }[][]
onchangeThe range changed: once, when it is complete.(value, text: [string, string]) => void—
oncalendarchangeAn end was chosen; range says which.(value, text, info: { range }) => void—
onokA range was confirmed with OK or Enter, with needConfirm.(value) => void—
onfocusThe focus came to the field or its panel from outside.(event, info: { range }) => void—
onblurThe focus left both.(event, info: { range }) => void—
native attributesThe other attributes go to the root, a group: aria-label, data-*.HTMLAttributes<HTMLDivElement>—

Methods

MethodDescriptionType
focusMoves focus to the field; in a range, to the end that is asked. The panel does not open.(options?: FocusOptions) => void
blurRemoves focus from the field; the panel closes.() => void

Design tokens

TokenDescriptionDefault
--ads-time-picker-column-heightHeight of the columns.224px
--ads-picker-time-column-widthWidth of a column.56px
--ads-picker-time-item-heightHeight of a number.28px
--ads-picker-time-selected-bgThe chosen number.#e6f4ff
--ads-picker-cell-hover-bgA number under the pointer.rgba(0, 0, 0, 0.04)
--ads-picker-popup-radiusCorner radius of the panel.8px
--ads-picker-popup-bgBackground of the panel.#ffffff
--ads-picker-popup-shadowShadow of the panel.0 6px 16px 0 rgba(0, 0, 0, 0.08), 0 3px 6px -4px rgba(0, 0, 0, 0.12), 0 9px 28px 8px rgba(0, 0, 0, 0.05)
--ads-picker-actions-heightLeast height of the buttons under the columns.42px
--ads-picker-presets-widthWidth of the column of quick choices.120px
--ads-picker-icon-colorThe icons of the field.rgba(0, 0, 0, 0.45)

Keyboard

  • Arrow Down in the field opens the panel and moves the focus to the first column.
  • In a column Up and Down change the number, Home and End go to its ends, Left and Right to the column beside.
  • Enter or Space chooses a number; Enter on the chosen one confirms what waits for OK. In a range the focus then goes to the input of the other end.
  • Escape closes the panel, drops what was not confirmed and returns the focus to the field.
  • Tab leaves the field and closes the panel; in a range it goes from the start to the end first. In the panel it walks the columns (one stop each) and the footer.

Accessibility

  • The field is a combobox that names its dialog; each column is a listbox with a name, and its numbers are options that say what is chosen and what is disabled.
  • A range is a group with two named inputs; the dialog says which end it asks for. Give the group a name with aria-label.
  • The default light colors follow Ant Design and not all reach a contrast of 4.5:1; load tokens-accessible.css for them. The chosen and disabled numbers stay visible in forced colors.
  • Checked with a real screen reader (Orca with Firefox): it reads the field, the name of the dialog, the columns and their numbers, the value after it is confirmed and the ends of a range. Orca does not say the value of such a field by itself, so every input also gives it as its description: NVDA, JAWS and VoiceOver, not checked, may say it twice. Orca does not say that a number is disabled, and for the Enter that chooses a number it says only the key. Not checked with NVDA, JAWS, VoiceOver or on a phone.