29

I am trying to disable all dates in a datepicker and only enable dates which are in an array. This is the code I have so far http://jsfiddle.net/peter/yXMKC/ the problem is only May 14th shows up as enabled. The others are all disabled. Any ideas?

var availableDates = ["9-5-2011","14-5-2011","15-5-2011"];

function available(date) {
  dmy = date.getDate() + "-" + (date.getMonth()+1) + "-" + date.getFullYear();
  if ($.inArray(dmy, availableDates) == 1) {
    return [true, "","Available"];
  } else {
    return [false,"","unAvailable"];
  }
}

$('#date').datepicker({ beforeShowDay: available });
1
  • $.inArray will return the position the value in the array(starts from 0), if the value is not in the array, it will return -1 Commented May 27, 2015 at 7:55

1 Answer 1

55

$.inArray(dmy, availableDates) returns the index of the element, so when you compare with 1 only 14-5-2011 will match. Check for not equal to -1. Should work.

Fiddle - http://jsfiddle.net/yXMKC/4/

var availableDates = ["9-5-2011","14-5-2011","15-5-2011"];

function available(date) {
  dmy = date.getDate() + "-" + (date.getMonth()+1) + "-" + date.getFullYear();
  console.log(dmy+' : '+($.inArray(dmy, availableDates)));
  if ($.inArray(dmy, availableDates) != -1) {
    return [true, "","Available"];
  } else {
    return [false,"","unAvailable"];
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

i want to ask you if i added another date say "22-10-2014" and other dates it will not be working. It will show only the selected moth date how to handle this
Worked perfectly for me !

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.