timepicker-ui
4.6.0GitHub
timepicker-ui
API Reference / Methods

Methods API Reference

Every method, getter and static method of DatepickerUI from @pglejzer/datepicker-ui, the companion of timepicker-ui.

The examples on this page use a picker created like this. The first argument of the constructor is a selector, an <input> or an element that contains an <input>. The options argument is optional.

index.ts
import DatepickerUI from "@pglejzer/datepicker-ui";
import "@pglejzer/datepicker-ui/index.css";
const picker = new DatepickerUI("#date");
picker.create();

Instance methods

#

create()

Binds the input and triggers. Mounts the picker when ui.inline.enabled is set. Calling it again does nothing. The constructor throws when it finds no input, so call create() after the input exists.

index.ts
const picker = new DatepickerUI("#date");
picker.create();

open()

Opens the picker. Does nothing for an inline picker.

index.ts
picker.open();

close()

Closes the picker.

index.ts
picker.close();

destroy(options?)

Removes everything the picker added and restores the input. Clears the input text unless keepInputValue is true.

index.ts
picker.destroy();
picker.destroy({ keepInputValue: true });

getValue()

Returns the committed value, or null when nothing is committed or a range has no end yet. The shape depends on calendar.mode. value is ISO YYYY-MM-DD (YYYY-MM for month precision, YYYY for year precision), date is a Date at local midnight and formatted is the text as the input shows it.

index.ts
// calendar.mode: "single"
picker.getValue();
// { value: "2025-03-14", date: Date, formatted: "14/03/2025" }
// calendar.mode: "range"
picker.getValue();
// { start: DateValue, end: DateValue }
// calendar.mode: "multiple", sorted
picker.getValue();
// DateValue[]

setValue(value)

Sets the committed value and the input text. Pass null to clear. It does not emit confirm. A value that does not fit calendar.mode emits an error event with code invalid-date.

index.ts
// single: a Date or an ISO string
picker.setValue("2025-03-14");
picker.setValue(new Date(2025, 2, 14));
// range: { start, end }
picker.setValue({ start: "2025-03-14", end: "2025-03-20" });
// multiple: an array
picker.setValue(["2025-03-14", "2025-03-16"]);
// month precision accepts "2026-10" too
picker.setValue(null);

setOptions(options)

Merges a partial options object into the current options and applies it. Emits update. Changing calendar.mode empties the selection.

index.ts
picker.setOptions({ ui: { theme: "crane" } });
picker.setOptions({ calendar: { mode: "range" } });

setActiveEnd(end)

Range mode only. Sets which end the next pick fills, "start" or "end". It is ignored in other modes and after destroy(), emits no event and does not open the picker.

index.ts
picker.open();
picker.setActiveEnd("end");

getElement()

Returns the .dp-ui host element.

index.ts
const host = picker.getElement();

on(event, handler)

Subscribes to an event and returns a function that unsubscribes.

index.ts
const off = picker.on("confirm", ({ value }) => {
console.log(value);
});
off();

once(event, handler)

Subscribes for one call and returns an unsubscribe function.

index.ts
picker.once("open", ({ mode }) => {
console.log(mode);
});

off(event, handler?)

Unsubscribes one handler, or all handlers of the event when you pass none.

index.ts
picker.off("confirm", handler);
picker.off("confirm");

Active end in range mode

#

From the first setActiveEnd() call on, a pick follows the active end, like it does with ui.endInput. When "end" is active and the range has a start, a date on or after the start sets the end. Any other pick makes that date a new start. After each pick the active end moves to the next end.

open() starts every docked or modal session at "start", so call setActiveEnd() after open().

index.ts
const picker = new DatepickerUI("#date", {
calendar: { mode: "range" },
});
picker.create();
picker.open();
picker.setActiveEnd("end");

Getters

#
GetterTypeDescription
instanceIdstringThe id of the instance.
optionsDatepickerOptionsThe options as passed, with setOptions() patches merged.
isInitializedbooleantrue after create().
isDestroyedbooleantrue after destroy().

Static methods

#

DatepickerUI.getById(id)

Returns the instance created with behavior.id, or undefined.

index.ts
new DatepickerUI("#date", { behavior: { id: "trip-dates" } }).create();
const picker = DatepickerUI.getById("trip-dates");

DatepickerUI.getAllInstances()

Returns every live instance.

index.ts
const pickers = DatepickerUI.getAllInstances();

DatepickerUI.destroyAll()

Destroys every live instance.

index.ts
DatepickerUI.destroyAll();
#

Events lists what on() can subscribe to, and TypeScript has the types of the values these methods take and return.

Esc