ss888 ss888 - 3 months ago 33
jQuery Question

jQuery PickMeUp datepicker: disable array of dates

I'm using a jQuery datepicker plugin called PickMeUp.

I have the datepicker working but can't work out how to disable dates in it. My plan is to have an array of dates that would be disabled on the datepicker calendar.

I did manage to disable one date using the documentation from a previous version of the plugin, (http://www.eyecon.ro/datepicker/), but I can't figure out how to add an array of dates to it.

jQuery



$(document).ready(function(){
var now2 = new Date();
now2.addDays(-1);
now2.setHours(0,0,0,0);
$('input#cdate').pickmeup({
position : 'right',
mode : 'range',
render: function(date) {
return {
disabled: date.valueOf() == now2.valueOf()
}
}
});
});





Update



Below is the working code. (Many, many thanks to Niloct)

$(document).ready(function(){
var arDates = [new Date("2014-02-14").valueOf(),new Date("2014-02-11").valueOf(),new Date("2014-02-09").valueOf()];
$('input#cdate').pickmeup({
position : 'right',
mode : 'range',
render: function(date) {
return {
disabled: arDates.indexOf(date.valueOf()) != -1
}
}
});
});

Answer

Ok, just writing it down:

valueOf() is one of the methods of Date object that gets the number of milliseconds in a date (since 01/01/1970).

indexOf() is a method of the Array object that checks if an element is a member of an array.

So your first code is spot on, also you got the tweaks:

var arDates = [new Date("2014-02-14").valueOf(),new Date("2014-02-11").valueOf(),new Date("2014-02-09").valueOf()];

disabled: arDates.indexOf(date.valueOf()) != -1

What you may face is issues with timezones. For instance: new Date("2014-02-17") for me does not create a date in Feb 17th. It falls back 3 hours due to my timezone.

Comments