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.
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.
const picker = new DatepickerUI("#date");picker.create();
open()
Opens the picker. Does nothing for an inline picker.
picker.open();
close()
Closes the picker.
picker.close();
destroy(options?)
Removes everything the picker added and restores the input. Clears the input text unless keepInputValue is true.
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.
// 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", sortedpicker.getValue();// DateValue[]
setValue(value)
Sets the committed value and the input text. Pass null to clear. Any date can be "today". It does not emit confirm. A value that does not fit calendar.mode emits an error event with code invalid-date. While the picker is shown, it moves to the new value and emits navigate with source: "api" when the shown days change.
// single: a Date or an ISO stringpicker.setValue("2025-03-14");picker.setValue(new Date(2025, 2, 14));picker.setValue("today");// range: { start, end }picker.setValue({ start: "2025-03-14", end: "2025-03-20" });// multiple: an arraypicker.setValue(["2025-03-14", "2025-03-16"]);// month precision accepts "2026-10" toopicker.setValue(null);
clear()
Empties the value and the input text, and the draft too while the picker is shown. Emits clear with the value it removed. After create() it also fires input and change on the input. It does not emit confirm. It does nothing when there is neither a value nor a shown draft, and after destroy().
picker.on("clear", ({ previousValue }) => {console.log(previousValue);});picker.clear();
isSelectable(value)
Returns true when a value passes every selection rule: minDate, maxDate, disabled dates and weekdays, enabledDates, isDateDisabled, and the range length or multiple.max limits. A Date or a string, including "today", is checked as one day in any mode. In range mode a { start, end } object is checked as a range, and in multiple mode an array as a set of days. An invalid or empty value, or a shape that does not fit the mode, returns false. The value is not changed.
picker.isSelectable("2025-03-14");picker.isSelectable("today");// calendar.mode: "range"picker.isSelectable({ start: "2025-03-14", end: "2025-03-20" });// calendar.mode: "multiple"picker.isSelectable(["2025-03-14", "2025-03-16"]);
setOptions(options)
Merges a partial options object into the current options and applies it. Emits update, and navigate with source: "api" when a shown picker changes the days it shows. Changing calendar.mode empties the selection.
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.
picker.open();picker.setActiveEnd("end");
setView(view)
Switches to the calendar with "calendar" or to the text fields with "input". It works while the picker is shown and has the input view: every range picker, and a single picker with the modal or full-screen look (ui.layout), at day precision. Otherwise it does nothing. Emits switch:view when the view changes.
picker.open();picker.setView("input");
getView()
Returns the view shown, "calendar" or "input". A closed picker returns "calendar".
picker.getView();// "calendar" | "input"
goTo(date)
Shows the month of a date without selecting it. Takes a Date, an ISO string or "today". A date outside minDate and maxDate shows the month of the nearest day allowed. Emits navigate with source: "api" when the shown days change. It does nothing while the picker is closed. An invalid date emits error with code invalid-date.
picker.open();picker.goTo("2025-06-01");picker.goTo("today");
getVisibleRange()
Returns { from, to }, the first and last day the picker shows, or null while it is closed. Both are DateValue objects whose value is always YYYY-MM-DD. Shown outside days count. With month precision the range is the shown year, and with year precision it spans every year offered.
picker.open();const range = picker.getVisibleRange();// { from: DateValue, to: DateValue }
getElement()
Returns the .dp-ui host element.
const host = picker.getElement();
on(event, handler)
Subscribes to an event and returns a function that unsubscribes.
const off = picker.on("confirm", ({ value }) => {console.log(value);});off();
once(event, handler)
Subscribes for one call and returns an unsubscribe function.
picker.once("open", ({ mode }) => {console.log(mode);});
off(event, handler?)
Unsubscribes one handler, or all handlers of the event when you pass none.
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().
const picker = new DatepickerUI("#date", {calendar: { mode: "range" },});picker.create();picker.open();picker.setActiveEnd("end");
Getters
#| Getter | Type | Description |
|---|---|---|
instanceId | string | The id of the instance. |
options | DatepickerOptions | The options as passed, with setOptions() patches merged. |
isInitialized | boolean | true after create(). |
isDestroyed | boolean | true after destroy(). |
Static methods
#DatepickerUI.getById(id)
Returns the instance created with behavior.id, or undefined.
new DatepickerUI("#date", { behavior: { id: "trip-dates" } }).create();const picker = DatepickerUI.getById("trip-dates");
DatepickerUI.getAllInstances()
Returns every live instance.
const pickers = DatepickerUI.getAllInstances();
DatepickerUI.destroyAll()
Destroys every live instance.
DatepickerUI.destroyAll();
Related
#Events lists what on() can subscribe to, and TypeScript has the types of the values these methods take and return.