declare a variable with array and also a range of dates

Go To StackoverFlow.com

-2

I've updated my code here. I've put the range inside the unavailableDays function which I need to reference in beforeShowDays in datepicker to disable those days.

function unavailableDays(date) {

function createDateRange(first, last) {
var date = [];

for(var j = first; j < last; j.setDate(j.getDate() + 7))
dates.push(new Date(j.getTime()));

return date;
}

var disabledDays = createDateRange(new Date("1978-08-10"), new Date("1978-11-05"));

//date array to be disabled
var disabledDays = ["1963-3-10", "1963-3-17", "1963-3-24", "1963-3-31", "1965-9-18"];

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]; }
2012-04-05 18:40
by amespower
I've tried Colin, many times, but I need a reputation of +15 first. I'm a newbie so I guess I get that reputation when I answer questions - amespower 2012-04-05 19:03


0

function createDateRange(first, last) {
    var dates = [];

    for(var i = first; i < last; i.setDate(i.getDate() + 7))
        dates.push(new Date(i.getTime()));

    return dates;
}

var disabledDays = createDateRange(new Date("1978-08-10"), new Date("1978-11-05"));

sample

2012-04-05 18:58
by Jesse van Assen
I'm trying to incorporate this into a function to disable the days with beforeShowDay. Here is my code added to original post - amespower 2012-04-05 20:20
I would advise you to not place the array as hard-coded variables in your function, but wrap it in a function which takes the first and last date as a parameter. This way, it is way easier to modify if you ever need to change the dates or the date range - Jesse van Assen 2012-04-06 08:21
yes I've since done that, like Jesse's answer above. Not sure how to add this range, var and for statement, to the existing function "unavailableDays" so it works the same as the array (disabling the dates) - amespower 2012-04-06 23:10
I modified my answer to include it. You actually have the same result as you have now, but when you need to change the range, you only have to change a parameter instead of recalculating the entire range and pasting it in your code - Jesse van Assen 2012-04-07 08:07
Thanks Jesse. Very 101 question I'm sure but can I put this function within the unavailableDays function so it's all one and I can then use that function in the datepicker beforeshowDays? And the other question I have is if I have disableDays assigned an array of days already, do I have to use a different var name for hte createDateRange? Would I then put this function before the if in array part - amespower 2012-04-07 16:24
The result of the function fills the disabledDays array. You can also use the createDateRange function within the unavailableDays function, but by doing it this way you have two advantages: 1. you don't have to modify your (working) code. 2. It stores the dates, so they won't have to be generated each time you open the datepicker - Jesse van Assen 2012-04-08 08:36
but disableDays is also the array of dates. Can I use the same variable for both? I've tried using it inside and outside unavailableDays function and it only seems to disable the array, not the range. I'm confused about how I could have the function separate and still have it work in the datepicker code with beforeShowDays. Thanks for your posts - amespower 2012-04-08 20:30
Ads