I’m setting up something of an “appointment” scheduler using the jQuery UI Datepicker, but I have a few conditions that I needed to implement:
- Customer could not schedule on a Sunday
- Customer can’t schedule after 12pm on Saturday (have not implemented this)
- Can’t schedule next day if the current time is past 12pm
- Can’t schedule massively in advance (I chose +2 months)
- Can’t schedule for the current day (and obviously not the past)
Here is my current code:
$(document).ready(function() {
function noSundays(date) {
return [date.getDay() != 0, ''];
}
var timezone = "EST";
$.getJSON("http://json-time.appspot.com/time.json?tz="+timezone+"&callback=?",
function(data){
if (data.hour < 12) {
$("#datepicker").datepicker({
beforeShowDay: noSundays,
minDate: '+1d',
maxDate: '+2m' });
} else {
$("#datepicker").datepicker({
beforeShowDay: noSundays,
minDate: '+2d',
maxDate: '+2m' });
}
})
});
So we set noSundays, get the current time in EST from the JSON server and if it’s before 12PM we fire up .datepicker with minDate : +1d (tomorrow), if it’s after 12PM we use minDate : +2d. This code works but I feel like I’m doing something really wrong. I’m still relatively inexperienced and would love to know if there is a better way to write this.
Thank you.
It can be shorter like this