I updated my code with string dates created with new Date and added back in the if statement. This isn’t disabling the string or range though. I’ve added the datepicker code too.
function unavailableDays(date) {
function createDateRange(first, last) {
var dates = [];
for(var j = first; j < last; j.setDate(j.getDate() + 7)) {
dates.push(new Date(j.getTime()));
}
var alwaysDisabled = [new Date("1963-3-10T00:00:00"), new Date("1963-3-17T00:00:00"), new Date("1963-3-24T00:00:00"), new Date("1963-3-31T00:00:00"), new Date("1965-9-18T00:00:00")];
return dates.concat(alwaysDisabled);
}
var disabledDays = createDateRange(new Date("1978-8-10T00:00:00"), new Date("1978-11-5T00:00:00"));
var yy = date.getFullYear(), mm = date.getMonth(), dd = date.getDate();
for (i = 0; i < disabledDays.length; i++) {
if($.inArray(yy + '-' + (mm+1) + '-' + dd,disabledDays) != -1 || new Date() < date) {
return [false];
}
}
return [true];
}
$(document).ready(function (){
$('.selector').datepicker({
inline: true,
dateFormat: 'yy-mm-dd',
constrainInput: true,
changeYear: true,
changeMonth: true,
minDate: new Date(1940, 1-1, 1),
maxDate: new Date(2011, 10-1, 24),
beforeShowDay: unavailableDays,
onSelect: function(dateText, inst) {
$("#img").attr("src", "http://www.example.com" + dateText + ".jpg");
var chosenDates = $.datepicker.parseDate('yy-mm-dd', dateText);
var backToString = $.datepicker.formatDate('MM dd' + ',' + ' yy', chosenDates);
$('.info').html('You are viewing:' + '<br />' +
backToString).addClass('background');
}
});
});
There is no problem in having a function inside of another, JavaScript is pretty much based on that. You could also have separate functions, and it would work as well.
UPDATE
(Considering the
dateversusdatesissue is fixed)Here is what seems to be the problem (this is what your question seems to be really about, not functions inside of functions!):
createDateRangereturns an array ofDateobjects, but your other version (var disabledDays = ["1963-3-10", ...) is an array of strings (and in a format that cannot be parsed bynew Date(str)).Your current loop seems to be trying to deal with the this string version, and it works, but you want to
createDateRangeto always ignore certain dates (as far as I understand what you are saying). So, try this:http://jsfiddle.net/t4ahF/4/