tags:

views:

52

answers:

2
function getWeek(date:Date):Number
{
  var days:Array = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
  var year:Number = date.fullYearUTC;
  var isLeap:Boolean = (year % 4 == 0) && (year % 100 != 0)
       || (year % 100 == 0) && (year % 400 == 0);
  if(isLeap)
    days[1]  ;

  var d = 0;
  //month is conveniently 0 indexed.
  for(var i = 0; i < date.month; i  )
    d  = days[i];
  d  = date.dateUTC;

  var temp:Date = new Date(year, 0, 1);
  var jan1:Number = temp.dayUTC;
  /**
   * If Jan 1st is a Friday (as in 2010), does Mon, 4th Jan
   * fall in the first week or the second one?
   *
   * Comment the next line to make it in first week
   * This will effectively make the week start on Friday
   * or whatever day Jan 1st of that year is.
   **/
  d  = jan1;

  return int(d / 7);
}
+2  A: 

I can see 4 issues:

1) Having determined that date is in a leap year the code fails to adjust the number of days in February properly.

2) When counting the total number of days of complete months in the year prior to date the code is overwriting the running total instead of summing the days.

3) Similar to (2), when adding the number of days elapsed in the month in which date falls, the total is overwritten.

4) Again, similar to (2) & (3), when adjusting for the convention of whether to count a partial first week (or not) the total number of days is being overwritten instead of adjusted.

Ooh - late 5th entry: Your loop isn't incrementing properly.

Basically, just look for the gaps that have been left for you to fill in the answers.

robaker
+1  A: 

Wait a second. This is a duplicate of this

http://stackoverflow.com/questions/2362956/flex-how-to-get-week-of-year-for-a-date

Exact function but you've changed it. try copy pasting this - without editing.

Don't vote this response up - this is not a correct response. For the correct answer. follow the link and vote that.

Glycerine