npm install flatpickr
bower install flatpickr-calendar
Otherwise:
Download
Thenrequire('flatpickr')
, use wiredep, or otherwise load the necessary files.
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]);
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>
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; } ] });
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; } ] });
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>
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){ ... } });
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>"; } });
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 });
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 Option | Type | Default | Description |
---|---|---|---|
altFormat | string | "F j, Y" | Exactly the same as date format, but for the altInput field |
altInput | Boolean | false | Show the user a readable date (as per altFormat), but return something totally different to the server. |
altInputClass | String | "" | 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-control here. |
allowInput | boolean | false | Allows the user to enter a date directly input the input field. By default, direct entry is disabled. |
clickOpens | boolean | true | Clicking on the input opens the date (time) picker. Disable this if you wish to open the calendar manually with.open() |
dateFormat | string | "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. |
defaultDate | String/Date | null | Set the initial selected date. Same as preloading a date string into an input's value attribute, but can also handle a Date object. |
disable | array | [] | See Disabling dates |
enable | array | [] | See Enabling dates |
enableTime | boolean | false | Enables time picker |
enableSeconds | boolean | false | Enables seconds in the time picker. |
hourIncrement | integer | 1 | Adjusts the step for the hour input (incl. scrolling) |
inline | boolean | false | Displays the calendar inline |
maxDate | String/Date | null | The maximum date that a user can pick to. |
minDate | String/Date | null | The minimum date that a user can start picking from. |
minuteIncrement | integer | 5 | Adjusts the step for the minute input (incl. scrolling) |
nextArrow | string | > | Code for the arrow icon, used to switch months. |
noCalendar | boolean | false | Hides the calendar. For use along with enableTime. |
onChange | function, [functions] | null | Function(s) to trigger on every date selection. See Events API |
onClose | function, [functions] | null | Function(s) to trigger on every time the calendar is closed. See Events API |
onOpen | function, [functions] | null | Function(s) to trigger on every time the calendar is opened. See Events API |
onReady | function, [functions] | null | Function to trigger when the calendar is ready. See Events API |
parseDate | function | false | Function that expects a date string and must return a Date object |
prevArrow | string | < | Code for the left arrow icon. |
shorthandCurrentMonth | boolean | false | Show the month using the shorthand version (ie, Sep instead of September). |
static | boolean | false | Position the calendar inside the wrapper and next to the input element. (Leavefalse unless you know what you're doing. |
time_24hr | boolean | false | Displays time picker in 24 hour mode without AM/PM selection when enabled. |
utc | boolean | false | When true, dates will parsed, formatted, and displayed in UTC. It's recommended that date strings contain the timezone, but not necessary. |
weekNumbers | boolean | false | Enables display of week numbers in calendar. |
wrap | Boolean | false | Custom elements and input groups |