-->

JQuery get list of available date objects from dat

2019-09-01 05:24发布

问题:

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.

回答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);