It might be a good idea to re-cast the problem so you can get the day number in the current year, you'll be able to use it for more date based calculations if you have to. That's most easily done with a static array of ints and a bit of arithmetic...
public static var month_days:Array = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
public static day_num(Date d):int
{
var cumulative_days:int = 0;
for (int i = 0; i < d.month; i++)
{
cumulative_days += month_days[i];
}
cumulative_days += d.date;
if (add_leap_day(d)) cumulative_days++;
return cumulative_days;
}
public static week_num(Date d):int
{
var int day_num = day_num(d);
return Math.floor(day_num/7);
}
public static function add_leap_day(Date d):boolean
{
// I'll let you work this out for yourself
// bear in mind you don't just need to know whether it's a leap year...
}
Beware of a few things:
- Does your definition of a calendar week start on 1st Jan or the first Sunday of the year?
- What do you do about the spare days in the year, especially leap years? (52x7 != 365)
- Are you interested in the calendar or fiscal year?
- Is the definition of a week the same for every week of your year? Sometimes Christmas has two weeks or an extra month!
I wound up writing a whole library of date based arithmetic functions for fiscal and regular calendars. It is a tricky problem if you are operating in any environment where your user has their own version of the calendar - i.e. any financial application at all.