timepicker-ui
4.6.0GitHub
timepicker-ui
Guides / Selection Modes

Single, Range and Multiple Date Selection

calendar.mode sets what @pglejzer/datepicker-ui, the companion of timepicker-ui, lets the user select. getValue() returns a different shape for each mode.

Selection modes

#

calendar.mode is "single" (the default), "range" or "multiple".

ModegetValue() returnssetValue() accepts
"single"DateValue or nullDate, ISO string "2025-03-14", null
"range"{ start: DateValue, end: DateValue } or null{ start, end } of dates, null
"multiple"DateValue[] (sorted) or nullan array of dates, null

DateValue has three fields. getValue() returns null when nothing is committed or a range has no end yet. setValue() emits an error event with the code invalid-date when the value does not fit the mode, and it does not emit confirm.

index.ts
type DateValue = {
value: string; // ISO YYYY-MM-DD
date: Date; // local midnight
formatted: string; // the text as the input shows it
};

Single date

#
Single
import DatepickerUI from "@pglejzer/datepicker-ui";
import "@pglejzer/datepicker-ui/index.css";
const picker = new DatepickerUI("#date", { calendar: { mode: "single" } });
picker.create();
picker.setValue("2025-03-14");
picker.getValue();
// { value: "2025-03-14", date: Date, formatted: "14/03/2025" }

Date range

#

calendar.range.minLength and calendar.range.maxLength bound a range in days, both ends included. The demo accepts 2 to 14 days.

Range
import DatepickerUI from "@pglejzer/datepicker-ui";
import "@pglejzer/datepicker-ui/index.css";
const picker = new DatepickerUI("#range", {
calendar: { mode: "range", range: { minLength: 2, maxLength: 14 } },
});
picker.create();
picker.setValue({ start: "2025-03-14", end: "2025-03-20" });
picker.getValue();
// { start: DateValue, end: DateValue }

Multiple dates

#

calendar.multiple.max bounds the number of dates. Clicking a selected day removes it. behavior.closeOnSelect is not used in this mode.

Multiple
import DatepickerUI from "@pglejzer/datepicker-ui";
import "@pglejzer/datepicker-ui/index.css";
const picker = new DatepickerUI("#dates", {
calendar: { mode: "multiple", multiple: { max: 5 } },
});
picker.create();
picker.setValue(["2025-03-14", "2025-03-16"]);
picker.getValue();
// DateValue[], sorted

Limits

#
  • calendar.range.minLength and calendar.range.maxLength for ranges.
  • calendar.multiple.max for multiple dates.
  • calendar.minDate, maxDate, disabledDates, enabledDates, disabledWeekdays and isDateDisabled decide which days can be selected. See Disabled Dates.

Forms

#

When the input has a name, the picker moves the name to a hidden input placed after it, so a form submits ISO values instead of the displayed text. The hidden input is removed and the name restored on destroy().

index.html
<form>
<input id="date" name="birthday" />
<button type="submit">Send</button>
</form>
Hidden input value by mode
ModeHidden value
single2025-03-14
range2025-03-14/2025-03-20
multiple2025-03-14,2025-03-16

An incomplete range gives an empty string. The visible input gets input and change events when a value is committed, and a form reset restores the initial value. With ui.endInput, each field with a name gets its own hidden ISO input.

Initial value

#

On create(), the picker reads the existing input text, either in the serialized form above or in the display format.

#
Esc