Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save JRyven/fa82d01ee82e3d2bf6c2e3ce2bcc6be8 to your computer and use it in GitHub Desktop.
Save JRyven/fa82d01ee82e3d2bf6c2e3ce2bcc6be8 to your computer and use it in GitHub Desktop.
jQuery Datepicker set Unavailable, Available, Weekly date options
<script>
// set unavailable dates
var unavailableDates = ['26-11-2020','24-12-2020','25-12-2020'];
var availableDates = ['9-9-2020','25-11-2020'];
// check unavailable dates and verify if the weekday is approved
function unavailable(date) {
// beforeShowDay, get date
dmy = date.getDate() + "-" + (date.getMonth()+1) + "-" + date.getFullYear();
// inArray returns the index of the element, or -1, if element not found
// if today is found in unavailableDates, return false
if (jQuery.inArray(dmy, unavailableDates) != -1) {
return [false];
// if today is found in todays availableDates, return true
} else if (jQuery.inArray(dmy, availableDates) != -1) {
return [true];
} else {
// if day is Sunday, Saturday, Wednesday, diable the day
if( date.getDay() == 0 || date.getDay() == 3 || date.getDay() == 6 ) {
return [false];
} else{
return [true];
}
}
}
function partial_day_cutoff(date) {
// default to no orders in the same day
var minDate = 1;
// get the current datestamp
var date = new Date();
// get the current timestamp (military)
var current_hour = date.getHours();
// if the current timestamp is greater than 12PM, change the minDate to no orders within this and the next day
if( current_hour > 12 ) {
minDate = 2;
}
return minDate;
}
jQuery(document).ready(function($){
var minDate = partial_day_cutoff();
$("#thedate").datepicker({
minDate : minDate,
// maxDate : 14,
beforeShowDay: unavailable,
});
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment