Tuesday, May 15, 2007

Calculate Business Days with ActionScript

Here's a quick function for calculating business days with ActionScript 3. I've found similar functions in other languages but there doesn't seem to be anything out there yet for AS3...so here ya go!


public static const millisecondsPerDay:int = 1000 * 60 * 60 * 24;

public function businessDaysElapsed(date1:Date,date2:Date):int {
var numberOfDays:int = 0;

while (date1 < date2) {
//increment date by a day
date1.setTime(date1.getTime() + millisecondsPerDay);
//if day is between monday and friday, add one day
if (date1.day >= 1 && date1.day <=5) {
numberOfDays ++
}
}
return numberOfDays;
}

No comments: