Hi, I tried using the original list (with blackout dates) and reversed it to only show dates on a list (for employee onboarding this is a list of available orientation dates). Nothing I tried worked... have manipulated formats (mm-dd-yy vs yy-mm-dd) to no avail. Any suggestions?
And... the selected dates are actually blacked out but the day after them is available on the calendar. I had to subtract one day from each target date in my list for it to work - but this is not intuitive and it would be nice if the dates just worked as entered/intended.
I'm no JS expert, but I'm pretty savvy. This is proving to be a nightmare - I've found all 21 examples of date manipulation in these forums but nothing provides a simple solution for the simple task of providing "available" dates in the calendar based upon a list of dates :(
//create the array of blackout dates
var orientationDatesList = new Array();
//this function is run when the form is loaded
$(document).ready(function(){
//set the date range of the Date field between tomorrow and 365 days in the future
var today = new Date();
var startDate = new Date();
startDate.setDate(today.getDate() + 1);
//startDate.setDate(today.getDate());
var endDate = new Date();
endDate.setDate(today.getDate() + 365);
var minDate = $.datepicker.formatDate('yy-mm-dd', new Date(startDate));
var maxDate = $.datepicker.formatDate('yy-mm-dd', new Date(endDate));
$('.myLimitedDate input').attr('min',minDate);
$('.myLimitedDate input').attr('max',maxDate);
//loop through each date in the blackout date list, and add it to an array
orientationDatesList.length = 0;
$('.orientationDates option').each(function() {
orientationDatesList.push(formatDate(new Date($(this).val())));
});
//the following four function work together to disable any dates on the picker
//that are weekends or are on the orientationDatesList array
//they also add a validation (parsley) setting for invalid date entries
function isValidDate(n) {
var t = n.getDay();
var d = n.getDate();
// var string = jQuery.datepicker.formatDate('mm-dd-yy', n); //does not work
var string = jQuery.datepicker.formatDate('yy-mm-dd', n); //original
// return (t!=6 && t!=0 && orientationDatesList.indexOf(string) == -1); //exclude these dates (day +1)
// return (t!=6 && t!=0 && orientationDatesList.indexOf(string) == 1); //exclude these dates (does not work)
// return (t!=6 && t!=0); //exclude just weekends (this works!)
//for the code below to work, the dates in the date list must all be 1 day less than the desired date (!)
return (t!=6 && t!=0 && orientationDatesList.indexOf(string,0) >= 0); //include ONLY these dates
}
function checkDate(n) {
return[isValidDate(n),""];
}
function checkField(input, inst) {
if ($(input).closest('li').hasClass('myLimitedDate')) {
$(input).datepicker("option", {beforeShowDay: checkDate});
}
}
$.datepicker.setDefaults( {beforeShow: checkField} );
window.Parsley.addValidator('limiteddate', {
validateString: function(value) {
//IMPORTANT! this date format needs to match the format you set for the field on the layout page
// return isValidDate(moment(value, "YYYY-MM-DD").toDate());
return isValidDate(moment(value, "MM-DD-YYYY").toDate());
},
messages: {
en: 'Not an available date.'
}
});
$('.myLimitedDate input').attr('data-parsley-limiteddate','');
});
//this function is run when the lookups are completed.
$(document).on("onloadlookupfinished", function () {
//loop through each date in the blackout date list, and add it to an array
orientationDatesList.length = 0;
$('.orientationDates option').each(function() {
orientationDatesList.push(formatDate(new Date($(this).val())));
});
});
//function to return any date formatted as yyyy-mm-dd
function formatDate(date) {
var d = new Date(date),
month = '' + (d.getMonth() + 1),
day = '' + (d.getDate() +1),
year = d.getFullYear();
if (month.length < 2)
month = '0' + month;
if (day.length < 2)
day = '0' + day;
return [year, month, day].join('-');
//return [month, day, year].join('-'); //this causes calendar to be unavilable
}