Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery UI Datepicker - Multiple Date Selections

Is there a way to use the jQuery UI Datepicker widget to select multiple dates?

All help is appreciated! If its not possible to use the jquery UI datepicker then is there something similar that does?

like image 584
tarnfeld Avatar asked Sep 20 '09 21:09

tarnfeld


People also ask

How do I use two Datepickers on the same page?

Just adding a textbox with datepicker id will not work properly, you need separate id or class with separate jQuery functions. Where two jQuery datepicker functions are used, ie, first datepicker is for text input with id “datepicker1” and second datepicker for text input with id “datepicker2”.


2 Answers

I needed to do the same thing, so have written some JavaScript to enable this, using the onSelect and beforeShowDay events. It maintains its own array of selected dates, so unfortunately doesn't integrate with a textbox showing the current date, etc. I'm just using it as an inline control, and I can then query the array for the currently selected dates.
I used this code as a basis.

<script type="text/javascript"> // Maintain array of dates var dates = new Array();  function addDate(date) {     if (jQuery.inArray(date, dates) < 0)          dates.push(date); }  function removeDate(index) {     dates.splice(index, 1); }  // Adds a date if we don't have it yet, else remove it function addOrRemoveDate(date) {     var index = jQuery.inArray(date, dates);     if (index >= 0)          removeDate(index);     else          addDate(date); }  // Takes a 1-digit number and inserts a zero before it function padNumber(number) {     var ret = new String(number);     if (ret.length == 1)          ret = "0" + ret;     return ret; }  jQuery(function () {     jQuery("#datepicker").datepicker({         onSelect: function (dateText, inst) {             addOrRemoveDate(dateText);         },         beforeShowDay: function (date) {             var year = date.getFullYear();             // months and days are inserted into the array in the form, e.g "01/01/2009", but here the format is "1/1/2009"             var month = padNumber(date.getMonth() + 1);             var day = padNumber(date.getDate());             // This depends on the datepicker's date format             var dateString = month + "/" + day + "/" + year;              var gotDate = jQuery.inArray(dateString, dates);             if (gotDate >= 0) {                 // Enable date so it can be deselected. Set style to be highlighted                 return [true, "ui-state-highlight"];             }             // Dates not in the array are left enabled, but with no extra style             return [true, ""];         }     }); }); </script> 
like image 110
Tevin Avatar answered Sep 21 '22 12:09

Tevin


When you modifiy it a little, it works regardless which dateFormat you have set.

$("#datepicker").datepicker({                         dateFormat: "@", // Unix timestamp                         onSelect: function(dateText, inst){                             addOrRemoveDate(dateText);                         },                         beforeShowDay: function(date){                             var gotDate = $.inArray($.datepicker.formatDate($(this).datepicker('option', 'dateFormat'), date), dates);                             if (gotDate >= 0) {                                 return [false,"ui-state-highlight", "Event Name"];                             }                             return [true, ""];                         }                     });      
like image 21
chriszero Avatar answered Sep 19 '22 12:09

chriszero