flatpickr

A lightweight & powerful datetimepicker

Install

npm install flatpickr
bower install flatpickr-calendar

Otherwise:
Download 

Thenrequire('flatpickr'), use wiredep, or otherwise load the necessary files.

Use

There are multiple ways to create a Flatpickr instance. In all cases, config is optional. The return value will be the Flatpickr instance, or an array of instances.

flatpickr(".selector", {});
document.getElementById("myID").flatpickr({});
$(".calendar").flatpickr({}); // jQuery

Or pass in a node directly.

new Flatpickr(HTMLElement, [options]);

Discover

Mode:



Show JSON

Examples

Custom elements and input groups

flatpickr can parse an input group of textboxes and buttons, common in Bootstrap and other frameworks.

This permits additional markup, as well as custom elements to trigger the state of the calendar.

Mark your input element withdata-input(mandatory), and any additional buttons withdata-toggle,data-open,data-close, ordata-clear.

<p class="flatpickr" data-wrap="true" data-click-opens="false">
	<input placeholder="Pick date" data-input>

	<a class="input-button" data-toggle><i class="icon-calendar"></i></a>
	<a class="input-button" data-clear><i class="icon-close"></i></a>
</p>

Disabling dates

Disable a date interval, or a specific date.

document.getElementById("#disableRange").flatpickr({
	disable: [
		{ from: "2016-08-16", to: "2016-08-19" },
		"2016-08-24",
		new Date().fp_incr(30) // 30 days from now
	]
});

Or pass in a custom function and disable dates using any logic you want.

document.getElementById("#disableOddDays").flatpickr({
	disable: [
		function(date){ // disable odd days
			return date.getDate()%2 > 0;
		}
	]
});

Enabling dates

Use the enable[] option to disable all dates except the ones you specify.

Pass in date strings, Date objects, or functions.

document.getElementById("#enableNextSeven").flatpickr({
	enable: [
		{
			from: "today",
			to: new Date().fp_incr(7) // 7 days from now
		}
	]
});

Use custom logic to enable the dates you need. For instance, to enable business days of 2016:

document.getElementById("#enableCustom").flatpickr({
	enable: [
		function(dateObj){
			// dateObj is a JavaScript Date
			return dateObj.getDay() %6 !== 0 && dateObj.getFullYear() === 2016;
		}
	]
});

UTC mode

By default, JavaScript's Date converts all dates to a local time. However, locale-agnostic databases often store dates in UTC. flatpickr can convert any given dates to UTC and select a datetime in UTC with a simple switch. Here are the previous examples with UTC mode enabled.

<input type='text' class='flatpickr' data-utc=true data-default-date="Wed, 21 Sep 2016 18:33:47 GMT" data-enable-time=true>


<input type='text' class='flatpickr' data-default-date="2016-12-27T16:16:22.585Z" data-utc=true data-enable-time=true>


Events

document.getElementById("events-api-example").flatpickr({
	minDate: "today",
	enableTime: true,
	onChange: function(dateObj, dateStr, instance) {
		...
	},
	onOpen: function(dateObj, dateStr, instance){
		...
	},
	onClose: function(dateObj, dateStr, instance){
		...
	}
});


onDayCreate()

Take full control of every date cell with theonDayCreate()hook.

flatpickr("#dayCreate", {
	onDayCreate: function(dObj, dStr, fp, dayElem){
		// utilize fp.currentYear, fp.currentMonth, dayElem.textContent
		if (Math.random() < 0.15)
			dayElem.innerHTML += "<span class='event'></span>";

		else if (Math.random() > 0.85)
			dayElem.innerHTML += "<span class='event busy'></span>";
	}
});


Fiscal Calendar

You may override thegetWeek()function, used for calculating a week number, for various purposes. A fiscal year is used for calculating yearly financial statements. In this example, we will use override thegetWeek()function to display the fiscal term instead of the usual week numbers.

<input type='text' id="fiscal" placeholder="Fiscal Calendar">
Flatpickr.prototype.getWeek = function(givenDate){
	var checkDate = new Date(givenDate.getTime());
	checkDate.setDate(checkDate.getDate() + 4 - (checkDate.getDay() || 7));
	var time = checkDate.getTime();
	checkDate.setMonth(7);
	checkDate.setDate(28);
	var week = (Math.floor(Math.round((time - checkDate) / 86400000) / 7) + 2);
	if (week < 1) {
		week = 52 + week;
	}
	return 'FW'+week;
}

document.getElementById("fiscal").flatpickr({
	weekNumbers: true
});


Options

Protip:all of the string/boolean config options can also be supplied using data attributes, e.g. data-min-date, data-default-date, data-date-format etc.

Config OptionTypeDefaultDescription
altFormatstring"F j, Y"Exactly the same as date format, but for the altInput field
altInputBooleanfalseShow the user a readable date (as per altFormat), but return something totally different to the server.
altInputClassString""This class will be added to the input element created by the altInput option. Helpful if input elements are styled using classes. Bootstrap users will want to specifyform-controlhere.
allowInputbooleanfalseAllows the user to enter a date directly input the input field. By default, direct entry is disabled.
clickOpensbooleantrueClicking on the input opens the date (time) picker. Disable this if you wish to open the calendar manually with.open()
dateFormatstring"Y-m-d"A string of characters which are used to define how the date will be displayed in the input box. The supported characters are defined in the table below.
defaultDateString/DatenullSet the initial selected date. Same as preloading a date string into an input's value attribute, but can also handle a Date object.
disablearray[]See Disabling dates
enablearray[]See Enabling dates
enableTimebooleanfalseEnables time picker
enableSecondsbooleanfalseEnables seconds in the time picker.
hourIncrementinteger1Adjusts the step for the hour input (incl. scrolling)
inlinebooleanfalseDisplays the calendar inline
maxDateString/DatenullThe maximum date that a user can pick to.
minDateString/DatenullThe minimum date that a user can start picking from.
minuteIncrementinteger5Adjusts the step for the minute input (incl. scrolling)
nextArrowstring>Code for the arrow icon, used to switch months.
noCalendarbooleanfalseHides the calendar. For use along with enableTime.
onChangefunction, [functions]nullFunction(s) to trigger on every date selection. See Events API
onClosefunction, [functions]nullFunction(s) to trigger on every time the calendar is closed. See Events API
onOpenfunction, [functions]nullFunction(s) to trigger on every time the calendar is opened. See Events API
onReadyfunction, [functions]nullFunction to trigger when the calendar is ready. See Events API
parseDatefunctionfalseFunction that expects a date string and must return a Date object
prevArrowstring<Code for the left arrow icon.
shorthandCurrentMonthbooleanfalseShow the month using the shorthand version (ie, Sep instead of September).
staticbooleanfalsePosition the calendar inside the wrapper and next to the input element. (Leavefalseunless you know what you're doing.
time_24hrbooleanfalseDisplays time picker in 24 hour mode without AM/PM selection when enabled.
utcbooleanfalseWhen true, dates will parsed, formatted, and displayed in UTC. It's recommended that date strings contain the timezone, but not necessary.
weekNumbersbooleanfalseEnables display of week numbers in calendar.
wrapBooleanfalseCustom elements and input groups