Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery datepicker not showing next years dates

I'm trying to show a list of start dates but for some reason my function only seems to show dates from this year and not any from 2016. I've added the code below along with a jsfiddle link. Any help with this would be great, I've been looking everywhere on how to get this to work but have found anything that can help with next years dates.

HTML

<div id="date"></div>

jQuery

var availableDates = ["25-12-2015", "02-01-2016"];

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

var lastDate = new Date("2016-10-31");  

$('#date').datepicker({
  dayNamesMin: ['S', 'M', 'T', 'W', 'T', 'F', 'S'],
  firstDay: 1,
  beforeShowDay: available,
  minDate: "-0M",
  maxDate: lastDate
});

jsfiddle

like image 736
huddds Avatar asked Dec 06 '25 05:12

huddds


1 Answers

The problem is getDate() for the 2nd would return 2 instead of 02. And the same with getMonth().

Either change your availableDates to:-

var availableDates = ["25-12-2015", "2-1-2016"];

or replace dmy like so:-

dmy = ("0" + date.getDate()).slice(-2) + "-" + ("0" + (date.getMonth()+1)).slice(-2) + "-" + date.getFullYear();

Fiddle

like image 98
BenG Avatar answered Dec 08 '25 19:12

BenG