Description
Light (~36kb minified js file and ~9kb gziped) customizable cross-browser calendar, built withes5
andcss flexbox
.Works in all modern browsers:
IE 10+, Chrome, Firefox, Safari 8+, Opera 17+.
Light (~36kb minified js file and ~9kb gziped) customizable cross-browser calendar, built withes5
andcss flexbox
.Works in all modern browsers:
IE 10+, Chrome, Firefox, Safari 8+, Opera 17+.
bower i --save air-datepicker
Or you can download files directly from GitHub
Include styles and scripts from/dist
directory:
<html>
<head>
<link href="dist/css/datepicker.min.css" rel="stylesheet" type="text/css">
<script src="dist/js/datepicker.min.js"></script>
<!-- Include English language -->
<script src="dist/js/i18n/datepicker.en.js"></script>
</head>
</html>
Datepicker will automatically initialize on elements with class.datepicker-here
, options may be sent viadata
attributes.
<input type='text' class="datepicker-here" data-position="right top" />
// Initialization
$('#my-element').datepicker([options])
// Access instance of plugin
$('#my-element').data('datepicker')
<input type='text' class='datepicker-here' data-language='en' />
Pass parameter{multipleDates: true}
for selection of multiple dates. If you want to limit the number of selected dates, pass the desired number{multipleDates: 3}
.
<input type="text"
class="datepicker-here"
data-language='en'
data-multiple-dates="3"
data-multiple-dates-separator=", "
data-position='top left'/>
Initialize plugin on non text input element, such as<div> … </div>
,or pass the parameter{inline: true}
.
<div class="datepicker-here" data-language='en'></div>
<input type="text"
class="datepicker-here"
data-language='en'
data-min-view="months"
data-view="months"
data-date-format="MM yyyy" />
To limit date selection, useminDate
andmaxDate
, they must receive JavaScript Date object.
$('#minMaxExample').datepicker({
language: 'en',
minDate: new Date() // Now can select only dates, which goes after today
})
Use{range: true}
for choosing range of dates. As dates separatormultipleDatesSeparator
will be used.
For possibility to select same date two times, you should set{toggleSelected: false}
.
<input type="text"
data-range="true"
data-multiple-dates-separator=" - "
data-language="en"
class="datepicker-here"/>
For disabling days, useonRenderCell
.
// Make Sunday and Saturday disabled
var disabledDays = [0, 6];
$('#disabled-days').datepicker({
language: 'en',
onRenderCell: function (date, cellType) {
if (cellType == 'day') {
var day = date.getDay(),
isDisabled = disabledDays.indexOf(day) != -1;
return {
disabled: isDisabled
}
}
}
})
Air Datepicker allows you to change contents of cells like you want. You could useonRenderCell
for this purpose.
Lets add extra elements to several dates, and show `lorem` text when selecting them.
var eventDates = [1, 10, 12, 22],
$picker = $('#custom-cells'),
$content = $('#custom-cells-events'),
sentences = [ … ];
$picker.datepicker({
language: 'en',
onRenderCell: function (date, cellType) {
var currentDate = date.getDate();
// Add extra element, if `eventDates` contains `currentDate`
if (cellType == 'day' && eventDates.indexOf(currentDate) != -1) {
return {
html: currentDate + '<span class="dp-note"></span>'
}
}
},
onSelect: function onSelect(fd, date) {
var title = '', content = ''
// If date with event is selected, show it
if (date && eventDates.indexOf(date.getDate()) != -1) {
title = fd;
content = sentences[Math.floor(Math.random() * eventDates.length)];
}
$('strong', $content).html(title)
$('p', $content).html(content)
}
})
// Select initial date from `eventDates`
var currentDate = currentDate = new Date();
$picker.data('datepicker').selectDate(new Date(currentDate.getFullYear(), currentDate.getMonth(), 10))
For adding some actions while datepicker is showing or hiding, useonShow
andonHide
callbacks.
$('#example-show-hide-callbacks').datepicker({
language: 'en',
onShow: function(dp, animationCompleted){
if (!animationCompleted) {
log('start showing')
} else {
log('finished showing')
}
},
onHide: function(dp, animationCompleted){
if (!animationCompleted) {
log('start hiding')
} else {
log('finished hiding')
}
}
})
To enable timepicker use option{timepicker: true}
- it will add current time and a couple of range sliders by which one can pick time.
By default current user time will be set. This value can be changed bystartDate
parameter.
<div class="datepicker-here" data-timepicker="true" data-language='en'></div>
More detailed info about timepicker parameters you can find in Options.
Time format is defined in localization object or intimeFormat
parameter. By default (in Russian language) 24 hours format is used. For enabling 12 hours mode you must addaa
orAA
symbol intimeFormat
. After what 'AM' and 'PM' sings will appear in timepicker widget.
Lets use 12 hours mode in Russian language:
<div class="datepicker-here" data-timepicker="true" data-time-format='hh:ii aa'></div>
For setting max/min hours or minutes values usemaxHours
,minHours
,maxMinutes
,minMinutes
. You also could set time inminDate
andmaxDate
. For setting hours you must use values between 0 and 23, event if 12 hours mode is on. Plugin will automatically transform given values to 12 hours format.
Lets create calendar where user can choose time between 09:00 am and 06:00 pm on working days and on Saturday and Sunday between from 10:00 am to 04:00 pm.
<input type='text' id='timepicker-actions-exmpl' />
<script>
// Create start date
var start = new Date(),
prevDay,
startHours = 9;
// 09:00 AM
start.setHours(9);
start.setMinutes(0);
// If today is Saturday or Sunday set 10:00 AM
if ([6, 0].indexOf(start.getDay()) != -1) {
start.setHours(10);
startHours = 10
}
$('#timepicker-actions-exmpl').datepicker({
timepicker: true,
language: 'en',
startDate: start,
minHours: startHours,
maxHours: 18,
onSelect: function (fd, d, picker) {
// Do nothing if selection was cleared
if (!d) return;
var day = d.getDay();
// Trigger only if date is changed
if (prevDay != undefined && prevDay == day) return;
prevDay = day;
// If chosen day is Saturday or Sunday when set
// hour value for weekends, else restore defaults
if (day == 6 || day == 0) {
picker.update({
minHours: 10,
maxHours: 16
})
} else {
picker.update({
minHours: 9,
maxHours: 18
})
}
}
})
</script>
You can add your localization to object$.fn.datepicker.language["my-lang"]
and pass it name to parameterlanguage
// Add custom localization
$.fn.datepicker.language['my-lang'] = {...}
// Initialize datepicker with it
$('.my-datepicker').datepicker({
language: 'my-lang'
})
You can also pass localization object directly inlanguage
$('.my-datepicker').datepicker({
language: {
days: [...]
...
}
})
If some fields are missing, they will be taken from default localization object ('Russian').
$.fn.datepicker.language['en'] = {
days: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
daysShort: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
daysMin: ['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa'],
months: ['January','February','March','April','May','June', 'July','August','September','October','November','December'],
monthsShort: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
today: 'Today',
clear: 'Clear',
dateFormat: 'mm/dd/yyyy',
timeFormat: 'hh:ii aa'
firstDay: 0
};
Available localizations located indist/js/i18n
directory.
Typestring
Defaults""
Extra css classes for datepicker.
Typeboolean
Defaultsfalse
If true, then datepicker will be always visible.
Typestring|object
Defaults"ru"
Datepicker's language. If string is passed, then language will be searched inDatepicker.language
object.
If object is passed, then data will be taken from this object directly.
If some fields are missing, they will be taken from default localization object ('Russian').
TypeDate
Defaultsnew Date()
This date will be shown at first initialization.
Typenumber
Defaults""
Day index from which week will be started. Possible values are from 0 to 6, where 0 - Sunday and 6 - Saturday. By default value is taken from current localization, but if it passed here then it will have higher priority.
Typearray
Defaults[6, 0]
Array of day's indexes which will be considered as weekends. Class.-weekend-
will be added to relevant cells.
. By default its Saturday and Sunday.
Typestring
Defaults""
Desirable date format. It's combination of d, m, yyyy, D, M, etc. By default value is taken from current localization, but if it passed here, then it will have higher priority.
Typestring|jQuery
Defaults""
Alternative text input. UsealtFieldDateFormat
for date formatting.
Typestring
Defaults"@"
Date format for alternative field.
Typeboolean
Defaultstrue
If true, then clicking on selected cell will remove selection.
Typeboolean
Defaultstrue
If true, then one can navigate through calendar by keyboard.
Hot keys:
Typestring
Defaults"bottom left"
Position of datepicker relative to text input. First value is name of main axis, and second is position on that axis.
For example{position: "right top"}
- will set datepicker's position from right side on top of text input.
Typenumber
Defaults12
Offset from the main positioning axes.
Typestring
Defaults"days"
Start datepicker view. Possible values are:
days
- display days of one monthmonths
- display months of one yearyears
- display years of one decadeTypestring
Defaults"days"
Minimal datepicker's view, on that view selecting cells will not trigger rendering next view, instead it will activate it.
Possible values are the same as inview
.
Typeboolean
Defaultstrue
If true, then days from other months will be visible.
Typeboolean
Defaultstrue
If true, then one can select days form other months.
Typeboolean
Defaultstrue
If true, then selecting days from other month, will cause transition to that month.
Typeboolean
Defaultstrue
If true, then years from other decades will be visible.
Typeboolean
Defaultstrue
If true, then on can select years from other decades
Typeboolean
Defaultstrue
If true, then selecting year from other decade, will cause transition to that decade.
TypeDate
Defaults""
The minimum date for selection. All dates, running before it can't be activated.
TypeDate
Defaults""
The maximum date for selection. All dates which comes after it cannot be selected.
Typeboolean
Defaultstrue
If true, then at the date, which would be less than minimum possible or more then maximum possible, navigation buttons ('forward', 'back') will be deactivated.
Typeboolean|number
Defaultsfalse
If true, then one can select unlimited dates. If number is passed, then amount of selected dates will be limited by it.
Typestring
Defaults","
Dates separator, which will be used when concatenating dates to string.
Typeboolean
Defaultsfalse
For selecting dates range, turn this option to true.multipleDatesSeparator
will be used as dates separator.
Typeboolean|Date
Defaultsfalse
If true, then button "Today" will be visible. If Date is passed then click event will also select passed date.
// Select today
$('.datepicker').datepicker({
todayButton: new Date()
})
Typeboolean
Defaultsfalse
If true, then button "Clear" will be visible.
Typestring
Defaults"focus"
Event type, on which datepicker should be shown.
Typeboolean
Defaultsfalse
If true, then after date selection, datepicker will be closed.
Typestring
Defaults<svg><path d="M 17,12 l -5,5 l 5,5"></path></svg>
Contents of 'next' button.
Typestring
Defaults<svg><path d="M 14,12 l 5,5 l -5,5"></path></svg>
Contents of 'prev' button.
Typeobject
Defaults
navTitles = {
days: 'MM, <i>yyyy</i>',
months: 'yyyy',
years: 'yyyy1 - yyyy2'
};
Content of datepicker's title depending on current view, can use same notation as in parameterdateFormat
. Missing fields will be taken from default values. Html tags are also possible.
$('#my-datepicker').datepicker({
navTitles: {
days: '<h3>Check in date:</h3> MM, yyyy'
}
})
Typestring
Defaults"monthsShort"
Field name from localization object which should be used as months names, when view is 'months'.
Typeboolean
Defaultsfalse
Iftrue
, when timepicker widget will be added.
Typestring
Defaults" "
Separator between date and time
Typestring
Defaultsnull
Desirable time format. Taken from localization by default. If value passed here, then it will be used instead.
For using 12 hours mode, add "aa" or "AA" to yourtimeFormat
parameter, e.g.{timeFormat: "hh:ii AA"}
Possible values are:
Typenumber
Defaults0
Minimal hours value, must be between 0 and 23. You will not be able to choose value lower than this.
Typenumber
Defaults23
Maximum hours value, must be between 0 and 23. You will not be able to choose value higher than this.
Typenumber
Defaults0
Minimal minutes value, must be between 0 and 59. You will not be able to choose value lower than this.
Typenumber
Defaults59
Maximum minutes value, must be between 0 and 59. You will not be able to choose value higher than this.
Typenumber
Defaults1
Hours step in slider.
Typenumber
Defaults1
Minutes step in slider.
Typefunction
Defaultsnull
Callback when selecting date
{multipleDates: true}
, then it will be an array of js dates.Typefunction
Defaultsnull
Callback when calendar is showing.
false
, when animation has just begun, iftrue
- already ended.Typefunction
Defaultsnull
Callback when calendar is hiding.
false
, when animation has just begun, iftrue
- already ended.Typefunction
Defaultsnull
Callback when months are changed.
Typefunction
Defaultsnull
Callback when year is changed
Typefunction
Defaultsnull
Callback when decade is changed
Typefunction
Defaultsnull
Callback when datepicker's view is changed
Typefunction
Defaultsnull
Callback when datepicker's cell is rendered.
The callback must return object which may consists of three fields:
{
html: '', // Custom cell content
classes: '', // Extra css classes to cell
disabled: '' // true/false, if true, then cell will be disabled
}
$('#my-datepicker').datepicker({
// Let's make a function which will add class 'my-class' to every 11 of the month
// and make these cells disabled.
onRenderCell: function(date, cellType) {
if (cellType == 'day' && date.getDate() == 11) {
return {
classes: 'my-class',
disabled: true
}
}
}
})
Plugin instance is accessible throughdata
attribute.
var myDatepicker = $('#my-elem').datepicker().data('datepicker');
myDatepicker.show();
Shows datepicker.
Hides datepicker.
Destroys datepicker.
Renders next month, year or decade, depending on current view.
Renders previous month, year or decade, depending on current view.
Date()
, or array of dates.Activates passed date or multiple dates if array is passed. If{multipleDates: false}
and date is already active, then it will be deactivated. If{multipleDates: true}
then another active date will be added.
Date()
Removes selection from passed date.
Clears all selected dates.
This method updates datepicker's options. After calling this method, datepicker will be redrawn. You can update several parameters at one time, just pass in object with necessary fields.
var datepicker = $('#my-elem').datepicker().data('datepicker');
// Single parameter update
datepicker.update('minDate', new Date())
// Multiple parameters
datepicker.update({
position: "top right",
maxDate: new Date(),
todayButton: true
})
Sets new view for datepicker.
datepicker.view = 'months';
Sets new viewing date for datepicker, must pass a JavaScript Date objectDate()
datepicker.date = new Date();
Datepicker's DOM element
Array of selected dates