0

I'm using Davidwalsh's method for blocking out specific dates on the datepicker calender, but on my production server certain dates in the array aren't being blocked off, so I tried to do a clean replicate of the code, and discovered the similar discrepancies in the results.

This is the JS fiddle

Can someone kindly point out something I'm missing here ?

For some reason, 1 November is blocked out, and 4th nov is not being blocked out.

On my production server's situation, we tried to block out 2 dates in december, but they fail to get disabled. Dates from the current month get blocked fine.

Any help would be greatly appreciated!

$(document).ready(function(){

        var cdates = new Array();
        cdates = ['2012-11-04','2012-11-12','2012-12-03','2012-12-12','2012-12-18','2012-12-20'];

        $('.datepicker').datepicker({
            beforeShowDay: check_closed
        }); 

        function check_closed(date)
        {               var m = date.getMonth(), d = date.getDate(), y = date.getFullYear();

            for (i = 0; i < cdates.length; i++) {
                if($.inArray(y + '-' + (m+1) + '-' + d,cdates) != -1 || new Date() > date) {
                    console.log('bad:  ' + (m+1) + '-' + d + '-' + y + ' / ' + cdates[i]);
                  return [false,''];
                }
                    console.log('good:  ' + (m+1) + '-' + d + '-' + y);
                return [true,''];    
            }                
        }
    });​

1 Answer 1

1

You are not padding day with a 0. For example, for the 1st of the month:

d = date.getDate(); // => 1 and for adequate comparison needs to be 01

The solution:

d = date.getDate();
if( d < 10 ) { d = "0" + d; }

That is why 4th of November is not disable, because var m = "2012-11-4" and the only value that exists in the array is "2012-11-04.

Here is a working example http://jsfiddle.net/bruno/KYzaR/8/

Sign up to request clarification or add additional context in comments.

2 Comments

My solution was added a if(d<10) { d = '0'+d; }
@Winterain You are absolutely right. Thanks for that, I have amended the answer. Going to upvote you :)

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.