flatpickr

A lightweight & powerful datetimepicker

Follow Star! Download

Install

Node Package Manager:
npm install flatpickr

Bower:
bower install flatpickr-calendar

Otherwise:
Download

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

<link rel="stylesheet" type="text/css" href="/path/to/flatpickr.css">
<script src="/path/to/flatpickr.js"></script>

Syntax

flatpickr('selector', [options]);

Basics

A basic datepicker
flatpickr('#flatpickr-tryme')

Multiple datetimepickers

The selector can be a class as well, turning multiple inputs/fields into calendars.

flatpickr('.calendar')
<input class=calendar placeholder="calendar 1">
<input class=calendar placeholder="calendar 2">
<input class=calendar placeholder="calendar 3">

Date Formatting

Using dateFormat

flatpickr('#yourSelector', { dateFormat: 'd-m-Y'});
flatpickr('#yourSelector', { dateFormat: 'm/d/Y'});
<input class=flatpickr data-dateFormat="l, F j, Y">

DateTime Picker

<input class=flatpickr data-enabletime>


DateTime Picker: 24 Hour Mode

<input class=flatpickr data-enabletime data-time_24hr>


DateTime Picker with seconds

<input class=flatpickr data-enabletime data-enableseconds data-dateFormat="Y-m-d h:i:S K">

Time Picker

<input class=flatpickr data-enabletime data-nocalendar value="09:00">

Date Limits - minDate and maxDate

The example below conveniently uses data-attributes for setting minDate and maxDate options.
<input class=flatpickr data-mindate=today>
<input class=flatpickr data-mindate="2016-09-20">
<input class=flatpickr data-mindate="September 2, 2015">
<input class=flatpickr data-mindate=today data-maxdate='2016-8-20'>

Human-friendly Date Output

altInput can be used for displaying a friendly date format (per altFormat) to the user while sending the date formatted as dateFormat to the server. Feel free to inspect the input element below after picking a date to see what's going on.

<input class=flatpickr data-altinput data-altFormat="F j, Y">

Selected date: nothing yet

Preload date and time

You may load the calendar with a predefined value, ranging from simple dates, such as "2016-04-10" to full-fledged datetime strings.

<input type=text class=flatpickr data-enableTime value="2016-03-01 03:30:00 -0300">


<input class=flatpickr data-defaultDate="2016-12-27T16:16:22.585Z" data-enableTime>

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 with data-input (mandatory), and any additional buttons with data-toggle, data-open, data-close, or data-clear.

<p class="flatpickr input-group" data-wrap data-clickOpens="false">

	<input placeholder="Pick date" data-input>

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

</p>

Disabling dates

Disable a date interval, or a specific date.

flatpickr('#disableRange', {
	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.

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


Disable all dates except the ones you need, by passing in date strings, Date objects, or functions.

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


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

flatpickr('#enableCustom', {
	enable: [
		function(dateObj){
			return dateObj.getDay() %6 !== 0 && dateObj.getFullYear() === 2016;
		}
	]
});

Allowing manual input

You may let your users input dates themselves simply by toggling the allowInput option

<input type=text class=flatpickr data-allowInput">

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 class=flatpickr data-utc data-defaultDate="2016-03-01 03:30:00 -0300" data-enableTime>


<input class=flatpickr data-defaultDate="2016-12-27T16:16:22.585Z" data-utc data-enableTime>

Event API

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


Example: check in and check out

var check_in = flatpickr("#check_in_date", {
	altInput: true,
	altFormat: "\\C\\h\\e\\c\\k \\i\\n\\: l, F j Y",
	minDate: new Date()
});
var check_out = flatpickr("#check_out_date", {
	altInput: true,
	altFormat: "\\C\\h\\e\\c\\k \\o\\u\\t: l, F j Y",
	minDate: new Date()
});

check_in.set("onChange", function(d) {
	check_out.set("minDate", d.fp_incr(1));
});
check_out.set("onChange", function(d) {
	check_in.set("maxDate", d.fp_incr(-1));
});

Display week numbers

<input class=flatpickr data-weeknumbers placeholder="Enabled week numbers">

Inline or embedded calendar

<input class=flatpickr data-inline placeholder="Pick a date below">

Customizing default options

// use your own arrow icons
flatpickr.init.prototype.defaultConfig.prevArrow = "<i class='icon i-angle-left'></i>";
flatpickr.init.prototype.defaultConfig.nextArrow = "<i class='icon i-angle-right'></i>";

// change the first day of the week to Monday
flatpickr.init.prototype.l10n.firstDayOfWeek = 1;

// then initialize your calendars
flatpickr(".flatpickr")
....

Options

Protip: all of the string/boolean config options can also be supplied using data attributes, e.g. data-mindate, data-defaultdate, data-dateformat 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 specify form-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 Object 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 [] Dates to disable, using intervals
enableTime boolean false Enables time picker
enableSeconds boolean false Enables seconds in the time picker.
noCalendar boolean false Hides the calendar. For use along with enableTime.
hourIncrement integer 1 Adjusts the step for the hour input (incl. scrolling)
minuteIncrement integer 5 Adjusts the step for the minute input (incl. scrolling)
inline boolean false Displays the calendar inline. See Inline or embedded calendar.
static boolean false Position the calendar inside the wrapper and next to the input element. (Leave false unless you know what you're doing.
wrap Boolean false The wrapping element. See Custom elements and input groups.
maxDate String null The maximum date that a user can pick to, as a string.
See Date.parse()
minDate String null The minimum date that a user can start picking from, as a string.
See Date.parse()
onChange function(dateObject, dateString) null A function that gets triggered on every date selection
onOpen function(dateObject, dateString) null A function that gets triggered on every time the calendar is opened.
onClose function(dateObject, dateString) null A function that gets triggered on every time the calendar is closed.
parseDate function false A function that expects a date string and must return a Date object
shorthandCurrentMonth boolean false Show the month using the shorthand version (ie, Sep instead of September).
weekNumbers boolean false Enables display of week numbers in calendar.
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.
prevArrow string < Code for the previous icon.
nextArrow string > Code for the next icon.

Date Format Characters

Character Description Example
d Day of the month, 2 digits with leading zeros 01 to 31
D A textual representation of a day Mon through Sun
l (lowercase 'L') A full textual representation of the day of the week Sunday through Saturday
j Day of the month without leading zeros 1 to 31
J Day of the month without leading zeros and ordinal suffix 1st, 2nd, to 31st
w Numeric representation of the day of the week 0 (for Sunday) through 6 (for Saturday)
F A full textual representation of a month January through December
m Numeric representation of a month, with leading zero 01 through 12
n Numeric representation of a month, without leading zeros 1 through 12
M A short textual representation of a month Jan through Dec
U The number of seconds since the Unix Epoch 1413704993
y A two digit representation of a year 99 or 03
Y A full numeric representation of a year, 4 digits 1999 or 2003

Time Format Characters

Character Description Example
H Hours (24 hours) 00 to 23
h Hours 1 to 12
i Minutes 00 to 59
S Seconds, 2 digits 00 to 59
s Seconds 0, 1 to 59
K AM/PM AM or PM

Escaping date format characters

To escape a character (if you need to use one of the reserved format characters above) use a double backslash: \\

Example:

dateFormat: '\\D\\a\\y \\p\\i\\c\\k\\e\\d: Y/m/d'

To get something like:

Day picked: 2013/02/12

If you do not escape the characters you would end up with something like this instead:

Tuea13 picke12: 2013/02/12

Which is probably not what you want...

Note: It's recommended that you escape all characters that you don't want accidentally converted to format characters in the future as others are added.

Localization

flatpickr supports localizing text in a similar way to modifying the methods (above).

Property Type Default Description
l10n.weekdays.shorthand array ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'] The shortened version of each weekday, starting with Sunday
l10n.weekdays.longhand array ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'] The long version of each weekday, starting with Sunday
l10n.months.shorthand array ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'] Shortened version of each month, starting with January
l10n.months.longhand array ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'] Long version of each month, starting with January
l10n.daysInMonth array [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] How many days in each month, starting with January
l10n.firstDayOfWeek integer 0 Start the calendar on a different weekday (0 = Sunday, 1 = Monday, 2 = Tuesday, etc.)

Example: weekdays in French:

<script>
	flatpickr.init.prototype.l10n.weekdays.longhand = ['dimanche', 'lundi', 'mardi', 'mercredi', 'jeudi', 'vendredi', 'samedi'];
	flatpickr('#yourId');
</script>

Table of Contents


    Theme: