jQuery Date Range Picker

jQuery Date Range Picker is a jQuery plugin that allows user to select a date range.

Requires jQuery 1.7+ (MIT LICENSE), Moment 2.8.1+ (MIT LICENSE).

Supports IE8+, Firefox, Chrome, Safari and other standard HTML5 browsers.

Supports multi-language, you can choose a defined language or set it to auto-detect mode.
You can also define new languages for it.

The HTML DOMs generated is fully CSS styled, you can change the skin by editting the CSS file easily.

I accept further customization job if you require more functions. My personal blog is http://jszen.com

Fork me on Github https://github.com/longbill/jquery-date-range-picker

Installation

<link rel="stylesheet" href="dist/daterangepicker.min.css">
<script type="text/javascript" src="moment.min.js"></script>
<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript" src="dist/jquery.daterangepicker.min.js"></script>
			

Demonstrations

  1. Default settings: Show Config
    {}
  2. Default settings with time enabled: Show Config
    {
    	startOfWeek: 'monday',
    	separator : ' ~ ',
    	format: 'DD.MM.YYYY HH:mm',
    	autoClose: false,
    	time: {
    		enabled: true
    	}
    }
  3. Default settings with default start/end time value: Show Config
    {
    	startOfWeek: 'monday',
    	separator : ' ~ ',
    	format: 'DD.MM.YYYY HH:mm',
    	autoClose: false,
    	time: {
    		enabled: true
    	},
    	defaultTime: moment().startOf('day').toDate(),
    	defaultEndTime: moment().endOf('day').toDate()
    }
  4. Default settings with default value: Show Config
    {}
  5. Force to Chinese: Show Config
    {
    	language:'cn'
    }
  6. Force to English: Show Config
    {
    	language:'en'
    }				
  7. Use custom language: Show Config
    {
    	language: 'custom'
    }			
  8. Select a date range after 2014-11-20: Show Config
    {
    	startDate: '2014-11-20'
    }				
  9. Limit date range selected between 3 days to 7 days: Show Config
    {
    	maxDays: 7,
    	minDays: 3
    }				
  10. Select a date range between 2013-01-10 to 2013-02-10: Show Config
    {
    	startDate: '2013-01-10',
    	endDate: '2013-02-10'
    }				
  11. Select a date range between 3 days and 7 days: Show Config
    {
    	minDays: 3,
    	maxDays: 7
    }			
  12. Set start of week to Monday: Show Config
    {
    	startOfWeek: 'monday'
    }			
  13. Use SPAN instead of INPUT: select Show Config
    {
    	getValue: function()
    	{
    		return this.innerHTML;
    	},
    	setValue: function(s)
    	{
    		this.innerHTML = s;
    	}
    }				
  14. Use two inputs: to Show Config
    {
    	separator : ' to ',
    	getValue: function()
    	{
    		if ($('#date-range200').val() && $('#date-range201').val() )
    			return $('#date-range200').val() + ' to ' + $('#date-range201').val();
    		else
    			return '';
    	},
    	setValue: function(s,s1,s2)
    	{
    		$('#date-range200').val(s1);
    		$('#date-range201').val(s2);
    	}
    }				
  15. Use another date format: Show Config
    {
    	format: 'dddd MMM Do, YYYY'  //more formats at http://momentjs.com/docs/#/displaying/format/
    }				
  16. Use future date shortcuts: Show Config
    {
    	showShortcuts: true,
    	shortcuts :
    	{
    		'next-days': [3,5,7],
    		'next': ['week','month','year']
    	}
    }				
  17. Use past date shortcuts: Show Config
    {
    	showShortcuts: true,
    	shortcuts :
    	{
    		'prev-days': [3,5,7],
    		'prev': ['week','month','year'],
    		'next-days':null,
    		'next':null
    	}
    }				
  18. Use custom shortcuts: Show Config
    {
    	showShortcuts: true,
    	shortcuts : null,
    	startOfWeek: 'sunday',
    	language:'en',
    	customShortcuts:
    	[
    		//if return an array of two dates, it will select the date range between the two dates
    		{
    			name: 'this week',
    			dates : function()
    			{
    				var start = moment().day(0).toDate();
    				var end = moment().day(6).toDate();
    				return [start,end];
    			}
    		},
    		//if only return an array of one date, it will display the month which containing the date. and it will not select any date range
    		{
    			name: 'Oct 2014',
    			dates : function()
    			{
    				//move calendars to show this date's month and next month
    				var movetodate = moment('2014-10','YYYY-MM').toDate();
    				return [movetodate];
    			}
    		}
    	]
    }				
  19. Use custom values: Show Config
    {
    	language:'en',
    	customValueLabel: 'Dynamic Ranges',
    	showCustomValues: true,
    	customValues:
    		//if return an array of two dates, it will select the date range between the two dates
    		[
    			{
    				name: 'MTD',
    				value: 'Month To Date'
    			},
    			{
    				name: 'YTD',
    				value: 'Year To Date'
    			}
    		]
    }				
  20. Auto-close after selection: Show Config
    {
    	autoClose: true
    }				
  21. In-line mode: Show Config
    {
    	inline:true,
    	container: '#date-range12-container',
    	alwaysOpen:true
    }				
  22. Single Date mode: Show Config
    {
    	autoClose: true,
    	singleDate : true,
    	showShortcuts: false
    }				
  23. Single Date mode with single month: Show Config
    {
    	autoClose: true,
    	singleDate : true,
    	showShortcuts: false,
    	singleMonth: true
    }				
  24. Batch mode ( week ): Show Config
    {
    	batchMode: 'week',
    	showShortcuts: false
    }				
  25. Batch mode ( week-range ): Show Config
    {
    	batchMode: 'week-range',
    	showShortcuts: false
    }				
  26. Disable some dates: Show Config
    {
    	showShortcuts: false,
    	beforeShowDay: function(t)
    	{
    		var valid = !(t.getDay() == 0 || t.getDay() == 6);  //disable saturday and sunday
    		var _class = '';
    		var _tooltip = valid ? '' : 'weekends are disabled';
    		return [valid,_class,_tooltip];
    	}
    }				
  27. Control by script:
    Show Config
    $('#date-range16').dateRangePicker(
    {
    	showShortcuts: false,
    	format: 'YYYY-MM-DD'
    }).bind('datepicker-change', function(evt, obj) {
    	alert('date1: ' + obj.date1 + ' / date2: ' + obj.date2);
    });
    
    $('#date-range16-open').click(function(evt)
    {
    	evt.stopPropagation();
    	$('#date-range16').data('dateRangePicker').open();
    });
    
    $('#date-range16-close').click(function(evt)
    {
    	evt.stopPropagation();
    	$('#date-range16').data('dateRangePicker').close();
    });
    
    $('#date-range16-set').click(function(evt)
    {
    	evt.stopPropagation();
    	$('#date-range16').data('dateRangePicker').setDateRange('2013-11-20','2014-08-25');
    });
    
    $('#date-range16-set-silent').click(function(evt)
    {
    	evt.stopPropagation();
    	$('#date-range16').data('dateRangePicker').setDateRange('2014-11-03','2015-02-12', true);
    });
    
    $('#date-range16-clear').click(function(evt)
    {
    	evt.stopPropagation();
    	$('#date-range16').data('dateRangePicker').clear();
    });
    
    $('#date-range16-destroy').click(function(evt)
    {
    	evt.stopPropagation();
    	$('#date-range16').data('dateRangePicker');
    });
    
    $('#date-range16-reset').click(function(evt)
    {
    	evt.stopPropagation();
    	$('#date-range16').data('dateRangePicker').resetMonthsView();
    });
    				
  28. Sticky months: Show Config
    {
    	stickyMonths: true,
    	startDate: '2013-01-10',
    	endDate: '2013-05-10'
    }				
  29. Use custom top bar: Show Config
    {
    	customTopBar: 'Foo Bar'
    }
  30. Different class names of first and second selected dates: Show Config
    {
    	extraClass: 'date-range-picker19'
    }
    .date-picker-wrapper.date-range-picker19 .day.first-date-selected { background-color: red !important; }
    .date-picker-wrapper.date-range-picker19 .day.last-date-selected { background-color: orange !important; }
  31. Hide hovering tooltip: Show Config
    {
    	hoveringTooltip: false
    }
  32. Customize hovering tooltip: Show Config
    {
    	hoveringTooltip: function(days)
    	{
    		var D = ['One','Two', 'Three','Four','Five'];
    		return D[days] ? D[days] : days;
    	}
    }
  33. Extra content on calendar: Show Config
    {
    	showDateFilter: function(time, date)
    	{
    		return '<div style="padding:0 5px;">\
    					<span style="font-weight:bold">'+date+'</span>\
    					<div style="opacity:0.3;">$'+Math.round(Math.random()*999)+'</div>\
    				</div>';
    	}
    }
  34. Single Month Mode: Show Config
    {
    	singleMonth: true,
    	showShortcuts: false,
    	showTopbar: false
    }
  35. Show Week Numbers: Show Config
    {
    	showWeekNumbers: true
    }
  36. Show Week Numbers (start from monday): Show Config
    {
    	showWeekNumbers: true,
    	startOfWeek: 'monday'
    }
  37. Show Week Numbers ( support fiscal year, start from 2015-08-16): Show Config
    {
    	showWeekNumbers: true,
    	getWeekNumber: function(day)
    	{
    		var fiscalYearStart = moment('2015-08-16','YYYY-MM-DD');
    		var daysOffset = parseInt(fiscalYearStart.format('DDD'),10);
    		return moment(day).add( -1*daysOffset, 'days').format('W');
    	}
    }
  38. Select forward: Show Config
    {
    	selectForward: true
    }
  39. Select backward: Show Config
    {
    	selectBackward: true
    }
  40. Typical usage, hotel booking: Show Config
    {
    	startDate: new Date(),
    	selectForward: true,
    	showDateFilter: function(time, date)
    	{
    		return '<div style="padding:0 5px;">\
    					<span style="font-weight:bold">'+date+'</span>\
    					<div style="opacity:0.3;">$'+Math.round(Math.random()*999)+'</div>\
    				</div>';
    	},
    	beforeShowDay: function(t)
    	{
    		var valid = !(t.getDay() == 0 || t.getDay() == 6);  //disable saturday and sunday
    		var _class = '';
    		var _tooltip = valid ? '' : 'sold out';
    		return [valid,_class,_tooltip];
    	}
    }				
  41. Custom open/close animation: Show Config
    {
    	customOpenAnimation: function(cb)
    	{
    		$(this).fadeIn(300, cb);
    	},
    	customCloseAnimation: function(cb)
    	{
    		$(this).fadeOut(300, cb);
    	}
    }
  42. Custom arrow symbol: Show Config
    // To make this demo work completely, the FontAwesome stylesheets are required
    {
        customArrowPrevSymbol: '<i class="fa fa-arrow-circle-left"></i>',
        customArrowNextSymbol: '<i class="fa fa-arrow-circle-right"></i>'
    }
    				
  43. Month and year select: Show Config
    {
        monthSelect: true,
        yearSelect: true
    }
    				
  44. Month and year select with year range by array: Show Config
    {
        monthSelect: true,
        yearSelect: [1900, moment().get('year')]
    }
    				
  45. Month and year select with year range by function: Show Config
    {
        monthSelect: true,
        yearSelect: function(current) {
            return [current - 10, current + 10];
        }
    }
                    
  46. Month and year select min and max dates: Show Config
    {
        monthSelect: true,
        yearSelect: true,
        startDate: moment().subtract(3, 'months').format('YYYY-MM-DD'),
        endDate: moment().endOf('day').format('YYYY-MM-DD'),
    }
    				

Configuration

Usage: $('#dom-id').dateRangePicker(configObject);

The default configuration object is:

{
	autoClose: false,
	format: 'YYYY-MM-DD',
	separator: ' to ',
	language: 'auto',
	startOfWeek: 'sunday',// or monday
	getValue: function()
	{
		return $(this).val();
	},
	setValue: function(s)
	{
		if(!$(this).attr('readonly') && !$(this).is(':disabled') && s != $(this).val())
		{
			$(this).val(s);
		}
	},
	startDate: false,
	endDate: false,
	time: {
		enabled: false
	},
	minDays: 0,
	maxDays: 0,
	showShortcuts: false,
	shortcuts:
	{
		//'prev-days': [1,3,5,7],
		//'next-days': [3,5,7],
		//'prev' : ['week','month','year'],
		//'next' : ['week','month','year']
	},
	customShortcuts : [],
	inline:false,
	container:'body',
	alwaysOpen:false,
	singleDate:false,
	lookBehind: false,
	batchMode: false,
	duration: 200,
	stickyMonths: false,
	dayDivAttrs: [],
	dayTdAttrs: [],
	applyBtnClass: '',
	singleMonth: 'auto',
	hoveringTooltip: function(days, startTime, hoveringTime)
	{
		return days > 1 ? days + ' ' + lang('days') : '';
	},
	showTopbar: true,
	swapTime: false,
	selectForward: false,
	selectBackward: false,
	showWeekNumbers: false,
	getWeekNumber: function(date) //date will be the first day of a week
	{
		return moment(date).format('w');
	},
	monthSelect: false,
	yearSelect: false
}

You can use the following keys in the configObject to overwrite the default configuration:

format (String)
	The date format string used for Moment.
	click here to see Moment documentation

separator (String)
	The separator string used between date strings

language (String)
	pre-defined languages are "en" and "cn", you can define your own
 	language then set this to the name of new language.
	You can also set this to "auto" to make it auto detect browser language.

startOfWeek (String)
	"sunday" or "monday"

getValue (Function)
	This function is called when get date range string from DOM
	When it is called, the context of this function is set to the datepicker DOM

setValue (Function)
	This function is called when set date range string to DOM

startDate (String or false)
	This string defines the earliest date which is allowed for the user, same format as `format`

endDate (String or false)
	This string defines the latest date which is allowed for the user, same format as `format`

minDays (Number)
	This number defines the minimum days of the selected range
	if this is 0, means do not limit minimum days

maxDays (Number)
	This number defines the maximum days of the selected range
	if this is 0, means do not limit maximum days

showShortcuts (Boolean)
	hide or show shortcuts area

shortcuts (Object)
	define the shortcuts buttons. there are some built in shortcuts, see source code

time (Object)
	If enabled adds time selection controls.

customShortcuts (Array)
	define custom shortcut buttons. see demo.js

inline (Boolean)
	whether to render the date range picker dom in inline mode instead of overlay mode,
	if set to true, please set `container` too

container (String, CSS selector || DOM Object)
	where should the date range picker dom should be renderred to

alwaysOpen (Boolean)
	if you use inline mode, you may want the date range picker widget to be renderred when the page loads
	set this to true will also hide the "close" button
	

singleDate (Boolean)
	choose a single date instead of a date range. If `singleMonth` option is set to true it will show
	only one month instead of two months.
	

batchMode (false / 'week' / 'month')
	 auto batch select mode 
	 false (default), week, month, week-range, month-range

beforeShowDay (Function)
	A function that takes a date as a parameter and must return an array with:
	[0]: true/false indicating whether or not this date is selectable
	[1]: a CSS class name to add to the date's cell or "" for the default presentation
	[2]: an optional popup tooltip for this date
	The function is called for each day in the datepicker before it is displayed.

stickyMonths (Boolean)
	If true, there will only be one previous and one next button. Clicking them will change
	both the months. This setting will have no effect if singleDate option is set to true

singleMonth (Boolean || 'auto') Default value: 'auto'
	If true, it will show only one month instead of two months. You can select date range
	in the one month view. If this is set to 'auto', it will be changed to true if the screen width
	is lower than 480.

showDateFilter ( Function(Int time, Int date) )
	This is a callback function when creating each date element in the calendar. First paramter will
	be the timestamp of that day. Second parameter will be the date of that month.

customTopBar ( Function || String)
	If you set this parameter, it will use this value in the top bar.

extraClass (String)
	Set extra class name to the date range picker dom.

customArrowPrevSymbol (String / String HTML)
	Set custom previous symbol, you can use html snippet too.

customArrowNextSymbol (String / String HTML)
	Set custom next symbol, you can use html snippet too.

showTopbar (Boolean)
	If show the top bar.

swapTime (Boolean)
	If true and if time is enabled, on choosing first enddate and than startdate, endtime and starttime will be swapped.
        If this configkey is false, only date will be swapped, time will stay constant.
        If time is disabled, this config key is not used.

selectForward (Boolean) Default: false
	If this is true, you can only select second date after the first selected date.

selectBackward (Boolean) Default: false
	If this is true, you can only select second date before the first selected date.

showWeekNumbers (Boolean) Default: false
	If this is true, it will show week number of the year in the calendar.

getWeekNumber (Function( Date object ) )
	the function called to generate the week number. the first parameter will be the first day of a week

monthSelect (Boolean) Default: false
	If this is true, you can quickly change month by clicking on month name

yearSelect (Boolean || Array || Function) Default: false
	If this is true, you can quickly change year by clicking on year number.
        By default select will contain years from "current year - 5" to "current year + 5" but you can change this.
        You can set year range by array like this [1900, 2017].
        And if you want more control you can set function which get selected year and should return array. For example:
            function(current) {
                return [current - 10, current + 10];
            }

Events

events will be triggerred on the date range picker DOM

$('#dom-id')
.dateRangePicker()
.bind('datepicker-first-date-selected', function(event, obj)
{
	/* This event will be triggered when first date is selected */
	console.log(obj);
	// obj will be something like this:
	// {
	// 		date1: (Date object of the earlier date)
	// }
})
.bind('datepicker-change',function(event,obj)
{
	/* This event will be triggered when second date is selected */
	console.log(obj);
	// obj will be something like this:
	// {
	// 		date1: (Date object of the earlier date),
	// 		date2: (Date object of the later date),
	//	 	value: "2013-06-05 to 2013-06-07"
	// }
})
.bind('datepicker-apply',function(event,obj)
{
	/* This event will be triggered when user clicks on the apply button */
	console.log(obj);
})
.bind('datepicker-close',function()
{
	/* This event will be triggered before date range picker close animation */
	console.log('before close');
})
.bind('datepicker-closed',function()
{
	/* This event will be triggered after date range picker close animation */
	console.log('after close');
})
.bind('datepicker-open',function()
{
	/* This event will be triggered before date range picker open animation */
	console.log('before open');
})
.bind('datepicker-opened',function()
{
	/* This event will be triggered after date range picker open animation */
	console.log('after open');
})

APIs

//after you called  $(dom).dateRangePicker();

$(dom).data('dateRangePicker')
    //set date range, two date strings should follow the `format` in config object,
    //set the third argument to be `true` if you don't want this method to trigger a `datepicker-change` event.
	.setDateRange('2013-11-20','2013-11-25');
	//set the start date to the specified date
	.setStart('2013-11-20');
	//set the end date to the specified date
	//set the second argument to `true` if you don't want this method to trigger a `datepicker-change` event.
	.setEnd('2013-11-25');
	.clear();		// clear date range
	.close();		// close date range picker overlay
	.open();		// open date range picker overlay
	.resetMonthsView();	// reset to default months
	.destroy();		// destroy all date range picker related things

License

This date range picker plugin is under MIT LICENSE.