The main flatpickr factory function and static utilities for creating and managing date picker instances.
function flatpickr(
selector: Node | ArrayLike<Node> | string,
config?: Options
): Instance | Instance[];Creates flatpickr instances from DOM elements or selectors.
Parameters:
selector: DOM element(s), NodeList, or CSS selector stringconfig: Optional configuration objectReturns:
Instance when selector matches one elementInstance[] when selector matches multiple elementsUsage:
// Single element
const picker = flatpickr("#date-input", { dateFormat: "Y-m-d" });
// Multiple elements
const pickers = flatpickr(".date-inputs", { mode: "range" });
// Direct element reference
const element = document.getElementById("my-date");
const picker = flatpickr(element, { enableTime: true });flatpickr.defaultConfig: Partial<ParsedOptions>;Global default configuration applied to all new instances.
// Set global defaults
flatpickr.defaultConfig = {
dateFormat: "Y-m-d",
time_24hr: true
};flatpickr.l10ns: { [k in LocaleKey]?: CustomLocale } & { default: Locale };Collection of available localization objects.
// Access available locales
console.log(flatpickr.l10ns.fr); // French locale
console.log(flatpickr.l10ns.default); // Default English localeflatpickr.localize: (l10n: CustomLocale) => void;Sets global localization by merging with default locale.
// Customize global locale
flatpickr.localize({
firstDayOfWeek: 1, // Monday
rangeSeparator: " - "
});flatpickr.setDefaults: (config: Options) => void;Sets global default configuration options.
// Set global defaults
flatpickr.setDefaults({
allowInput: true,
clickOpens: true,
time_24hr: true
});flatpickr.parseDate: (
date: DateOption,
format?: string,
timeless?: boolean
) => Date | undefined;Parses various date inputs into Date objects.
Parameters:
date: Date input (Date, string, or number)format: Optional format string for parsingtimeless: Optional flag to ignore time componentsconst parsed = flatpickr.parseDate("2023-12-25", "Y-m-d");
console.log(parsed); // Date object for Dec 25, 2023flatpickr.formatDate: (date: Date, format: string) => string;Formats Date objects into strings using flatpickr format tokens.
Parameters:
date: Date object to formatformat: Format string using flatpickr tokensconst formatted = flatpickr.formatDate(new Date(), "F j, Y");
console.log(formatted); // "December 25, 2023"flatpickr.compareDates: (
date1: Date,
date2: Date,
timeless?: boolean
) => number;Compares two Date objects, returning -1, 0, or 1.
Parameters:
date1: First date for comparisondate2: Second date for comparisontimeless: Optional flag to ignore time componentsconst result = flatpickr.compareDates(
new Date("2023-12-25"),
new Date("2023-12-26")
);
console.log(result); // -1 (date1 is before date2)When loaded in a browser, flatpickr automatically attaches to the global window object:
window.flatpickr === flatpickr; // trueIf jQuery is available, flatpickr automatically adds a jQuery plugin:
$('#date-input').flatpickr({
dateFormat: "Y-m-d",
enableTime: true
});Flatpickr extends the Date prototype with an increment method:
Date.prototype.fp_incr: (days: number | string) => Date;const tomorrow = new Date().fp_incr(1);
const nextWeek = new Date().fp_incr(7);