4

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.

Joe Doyle
  • 6,363
  • 3
  • 42
  • 45
enesness
  • 3,123
  • 5
  • 31
  • 33

4 Answers4

5

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);
}
Amarghosh
  • 58,710
  • 11
  • 92
  • 121
  • Thanks but this is not what I mean. I search for week of year which can be between 1st and 52nd week of year. For example 4th of March 2010 is 9th week of the year. – enesness Mar 02 '10 at 13:07
  • That's exactly what this function will do for you - pass a `new Date(2010, 2, 4);` to this function and see what it returns. – Amarghosh Mar 02 '10 at 13:21
  • @javanes Oops, there was a mistake in the function, fixed it. Try now. – Amarghosh Mar 02 '10 at 13:28
  • i guess the logic should return Math.ceil(d/7) instead of int. int will always make a date fall into the previous week, unless we were to assume that weeks start with week 0 – Rohan Feb 05 '14 at 14:56
1

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 :)

Flocoon
  • 11
  • 1
1

Before d is divided by 7, it should be decremented by 1. Otherwise, Saturday would go to next week.

Take 2011 as example, 1/1/2011 is Saturday. It should be week in 0, and 1/8/2011 should be in week 1.

If d is not decremented, then 1+6=7/7=1, and 8+6=14/7=2. So these are not correct.

rocksoccer
  • 71
  • 1
  • 7
1

I tried to use Amarghosh's function but I had issues with the UTC values. And with the first days of a year as well.

Therefore I modified the setting of jan1 (depending on sundays) and the final week calculation

Here is the function I use, based on Amarghosh's one :

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

    var d:Number = 0;

    for(var i:int = 0; i < (date.month); i++){
        d += days[i];
    }

    d += date.date;

    var temp:Date = new Date(year, 0, 1);
    var jan1:Number = temp.day;
    if(jan1 == 0) // sunday
        jan1 = 7;
    d += jan1 - 1;


    var week:int = int((d-1) / 7);

    if(week == 0) // les premiers jours de l'année
        week = 52;

    return (week < 10 ? "0" : "") + week;

}
Chris
  • 1,016
  • 1
  • 14
  • 18