0

I have the following datepicker set up:

<script>
     var disabled = ["2014-09-01","2014-10-16", "2014-10-17", "2014-10-20", "2014-11-26", "2014-11-27", "2014-11-28", "2014-12-22", "2014-12-23", "2014-12-24", "2014-12-25", "2\
    014-12-26", "2014-12-29", "2014-12-30", "2014-12-31", "2015-01-1", "2015-01-2", "2015-01-19", "2015-02-16", "2015-03-9", "2015-04-06", "2015-04-07", "2015-04-08", "2015-04-09"\
    , "2015-04-10", "2015-05-25" ] // yyyy-mm-dd


    $("#datepicker").datepicker({
        minDate: new Date(2014, 7, 19),
         maxDate: new Date(2015, 4, 29),
          numberOfMonths: 1,
           hideIfNoPrevNext: true,
            beforeShowDay: function(date){
             var string = jQuery.datepicker.formatDate('yy-mm-dd', date);
               return [ disabled.indexOf(string) == -1 ]
           }
       });
</script>
<div>
    <input type="text" id="datepicker"/>
</div>

Is there a way for me to form an array of each of the valid dates in this datepicker? (i.e. is there a built in method for doing this or something similar? Or will I need to write it manually?)

I'm doing this for the purpose of a scheduling application, I'm going to eventually turn this list into a hash with the keys being the elements of the list, and the values being hours in that day.

kingsfoil
  • 3,795
  • 7
  • 32
  • 56

1 Answers1

1

To get the array of available dates with javascript:

var min=new Date(2014, 7, 19);
var max=new Date(2015, 4, 29);
var available={};
var available_arr=[];

for(;min<=max;min.setDate(min.getDate()+1)){
    available[jQuery.datepicker.formatDate('yy-mm-dd', min)]=true;
}

for(var i=0;i<disabled.length;i++){
    available[disabled[i]]=false;
}


for(var i in available){
    if(available[i]) available_arr.push(i);
}

console.log(available_arr);
juvian
  • 15,875
  • 2
  • 37
  • 38
  • I'm a bit confused on the line: `min<=max;min.setDate(min.getDate()+1)` If I'm not mistaken, that says: "While min is less than or equal to max, take the min date object and call getDate() on it, that returns the number of the day in the month that min is set in, add one to that number and hand it as a parameter to min.setDate. Which will set the current date we're working with to the next date we need. Then, format the min date object nicely and put it into the hash called 'available' with min as the key and true as the value." Is that correct? If it is, how does the... – kingsfoil May 31 '14 at 00:33
  • ... `min.setDate(min.getDate()+1)` not eventually return an invalid date object? Or does it only create the array for the days in one month? – kingsfoil May 31 '14 at 00:34
  • @alex0112 the for does this: while min <= max (we will go day by day until min>max), set the day of min to 1 more. That is, to add a day to the date.Adding a day this way, js date object already takes care of changing month/year automatically. So basically, its a for of a range of dates, looping each day at a time. min.setDate(min.getDate()+1) will never return invalid date object, unless it reached the max date of Date object ;) – juvian May 31 '14 at 18:30