Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flex, how to get week of year for a date?

I could not find a method in Flex Date object to get the week of year (1 to 52)

What is the best way to find this? Is there any useful library for flex for such date operations like JodaTime in Java.

like image 298
enesness Avatar asked Feb 28 '23 11:02

enesness


2 Answers

I'm not aware of a library, but this function will get you the week index (zero based).

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);
}
like image 142
Amarghosh Avatar answered Mar 06 '23 18:03

Amarghosh


I just want to point out that there is an error in the above solution.

for(var i = 0; i < date.month; i++)

should be

for(var i = 0; i < date.monthUTC; i++)

in order to work properly.

Nevertheless thanks for the solution, it helped me a lot :)

like image 21
Flocoon Avatar answered Mar 06 '23 17:03

Flocoon