2

I'm creating a booking form that includes jQuery UI Datepicker, now i have a main issue that I need help with: Some trips will have only certain days active, and can only be sold certain months because, for example cyclone season.

The function to disable specifc weekdays works perfect, but i'm not sure how can I deactivate complete months for example October. Is it possible?

Any advices will be helpful, here is my code, please let me know if should I explain better my question, or if should I add some other parts of my code. Thanks in advance!

$.datepicker.setDefaults( $.datepicker.regional[ getLanguage() ] );

$( "#booking" ).datepicker({
   showOn: "button",
   dateFormat : 'yy/mm/dd',
   buttonText: "Select",
   beforeShowDay: disableSpecificWeekDays,
   changeMonth: true,
   changeYear: true,
   minDate: 0
}
);

// 0 = monday, 1 = tuesday, 2 = wednesday, 3 = thursday, 4=friday, 5 = saturday, 6=sunday
var daysToDisable = [1, 4, 6];

function disableSpecificWeekDays(date) {
            var day = date.getDay();
            for (i = 0; i < daysToDisable.length; i++) {
                if ($.inArray(day, daysToDisable) != -1) {
                    return [false];
                }
            }
            return [true];
 }

With this code for example i'm being able to deactivate specific dates, but to deactivate a whole month i'll need to write each date, and in certain cases, i'll need to deactivate at least three or four months for each trip.

var $disableMonths = new Array("2013/10/01","2013/10/02","2013/10/03", "...");

function disableSpecificMonths(mydate){
var $return = true;
var $returnclass ="available";

$checkdate = $.datepicker.formatDate('yy/mm/dd', mydate);

    for(var i = 0; i < $disableMonths.length; i++) {
            if($disableMonths[i] == $checkdate) {
                $return = false;
                $returnclass= "unavailable";
            }
    }

return [$return,$returnclass];

}

But also, if it's not possible to deactivate a complete month i'll love to get some ideas on how can I mix this two functions, so i'll be deactivating specific Week Days and Specific Dates.

1 Answer 1

4

You can update your "disable function" to something like this:

var daysToDisable = [1, 4, 6];
var monthsToDisable = [9];

function disableSpecificWeekDays(date) {
    var day = date.getDay();
    if ($.inArray(day, daysToDisable) != -1) {
        return [false];
    }

    var month = date.getMonth();
    if ($.inArray(month, monthsToDisable) != -1) {
        return [false];
    }
    return [true];
}
Sign up to request clarification or add additional context in comments.

9 Comments

Thanks, it definitely works, but not sure why it deactivates november instead of october. Thoughts?
Ah yes sorry, the month numbers are counted from 0 to 11, not 1 to 12. I updated the code.
Is there a specific reason to be counted from 0 to 11 instead of 1 to 12, i'm new on this, and i'd love to learn :)
it's ok i'm already doing a research about it, thank you so much!
Also, as you are using $.inArray, you don't need the for loop, see my updated code.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.