David David - 3 months ago 45
Javascript Question

jQuery Datepicker Beforeshowday two arrays of dates

I have one array for disabled dates and another for dates to be highlighted. Is there a way to code this?

e.g.:

beforeShowDay: function(date){
var unavilble = jQuery.datepicker.formatDate('yy/mm/dd', date);
var newstring = [ arrayable.indexOf(unavilble) == -1 ];
return newstring;
},


2nd array:

var datet = ['2014/01/19', '2014/01/20'];
//tips are optional but good to have
var tips = ['some description','some other description'];

function highlightDays(date) {
for (var i = 0; i < datet.length; i++) {
if (new Date(datet[i]).toString() == date.toString()) {
return [true, 'highlight', tips[i]];
}
}
return [true, ''];
}
beforeShowDay: highlightDays,

Answer

Try something like

var datet = ['2014/01/19', '2014/01/20'];
//tips are optional but good to have
var tips = ['some description', 'some other description'];
var arrayable = ['2014/01/01', '2014/01/08', '2014/01/15', '2014/01/22', '2014/01/29']
$('input').datepicker({
    dateFormat: 'yy/mm/dd',
    beforeShowDay: function (date) {
        var datestring = jQuery.datepicker.formatDate('yy/mm/dd', date);
        var hindex = $.inArray(datestring, datet);
        if (hindex > -1) {
            return [true, 'highlight', tips[hindex]];
        }
        var aindex = $.inArray(datestring, arrayable);
        return [aindex == -1]
    }
})

Demo: Fiddle