tags:

views:

51

answers:

2

so, i am trying to get my script to figure out the relevant last day of the school term. it needs to run until the current date is less than (as in before) the last day of the quarter. it just keeps on running no matter what. ps, i think the problem might be that it isnt resetting the "lastDayofQuarter" (at least outside of the loop). How can i circumvent this? thanks a ton!

var today = new Date();

var lastDayofQuarter=new Date();

var lastDaysofQuarter=new Array();

lastDaysofQuarter[0]=(2009,11,3); 
lastDaysofQuarter[1]=(2010,11,10);
lastDaysofQuarter[2]=(2011,4,18);
lastDaysofQuarter[3]=(2011,5,10);

lastDayofQuarter.setFullYear(lastDaysofQuarter[0]);

i=0;

while (lastDayofQuarter<today) {
 lastDayofQuarter.setFullYear(lastDaysofQuarter[i]);
 i++; 
}
A: 

setFullYear doesn't accept a Date as a parameter - it accepts one - three integers which represent the year, month and day of the month.

Your script will do what you use simply assign the value from your array to lastDayofQuarter, instead of using setFullYear:

lastDaysofQuarter[0]=new Date(2009,11,3); 
lastDaysofQuarter[1]=new Date(2010,11,10);
lastDaysofQuarter[2]=new Date(2011,4,18);
lastDaysofQuarter[3]=new Date(2011,5,10);

lastDayofQuarter = lastDaysofQuarter[0];

i=1;

while (lastDayofQuarter<today) {
 lastDayofQuarter = lastDaysofQuarter[i];
 i++; 
}
Dexter
Seems like you changed the sample code in your question after I posted, to no longer suggest that you were trying to pass a date to setFullYear - no matter, the above sample should work for you regardless.
Dexter
+1  A: 

These lines are syntactically valid, but they aren't working as you intend them to work:

lastDaysofQuarter[0]=(2009, 11, 3); 
// ...

The comma operator evaluates both of its operands (from left to right) and returns the value of the second operand. Therefore (2009, 11, 3) simply returns 3.

It looks you you want to do:

lastDaysofQuarter[0] = new Date(2009, 11, 3); 
// ...
Daniel Vassallo
great, that is exactly what i needed, thanks so much, also thanks for the tidbit about the commas in loops
@106058983: Glad it helped. You may want to mark the answer as accepted by clicking on the green tick on the left, if it was helpful.
Daniel Vassallo