A Flash Developer Resource Site

# Thread: ISO-8601 Week Number from Date()

1. ## ISO-8601 Week Number from Date()

I just thought you might find this useful.
Since AS does not feature a week number method in the Date() class, and it can be quite a mess to find...

Here it is, should clearly work the same in AS2 and AS3, I left it as a simple function:

Code:
```/**
*	ISO-8601 Week Number
*	Processes the week number of a date,
*	just like date("W") would do in PHP.
*
*	Procedure by Rick McCarty, 1999
*	Adapted to CVI by R.Bozzolo, 2006
*	Adapted to AS2 by A.Colonna, 2008
*
*/
function calculateISO8601WeekNumber(d:Date) {
// 1) Convert date to Y M D
var Y:Number = d.getFullYear();
var M:Number = d.getMonth();
var D:Number = d.getDate();

// 2) Find out if Y is a leap year
var isLeapYear:Boolean = (Y % 4 == 0  and  Y % 100 != 0) or Y % 400 == 0;

// 3) Find out if Y-1 is a leap year
var lastYear:Number = Y - 1;
var lastYearIsLeap:Boolean = (lastYear % 4 == 0  and  lastYear % 100 != 0) or lastYear % 400 == 0;

// 4) Find the Day of Year Number for Y M D
var month = [0,31,59,90,120,151,181,212,243,273,304,334];
var DayOfYearNumber:Number = D + month[M];
if(isLeapYear and M > 1)
DayOfYearNumber++;

// 5) Find the weekday for Jan 1 (monday = 1, sunday = 7)
var YY:Number = (Y-1) % 100; // ...
var C:Number = (Y-1) - YY; // get century
var G:Number = YY + YY/4; // ...
var Jan1Weekday:Number = 1 + (((((C / 100) % 4) * 5) + G) % 7);

// 6) Find the weekday for Y M D
var H:Number = DayOfYearNumber + (Jan1Weekday - 1);
var Weekday:Number = 1 + ((H -1) % 7);

var YearNumber = Y;
var WeekNumber;
// 7) Find if Y M D falls in YearNumber Y-1, WeekNumber 52 or 53
if (DayOfYearNumber <= (8-Jan1Weekday) and Jan1Weekday > 4)
{
trace('Date is within the last week of the previous year.');
YearNumber = Y - 1;
if (Jan1Weekday == 5 or (Jan1Weekday == 6 and isLeapYear))
{
WeekNumber = 53;
} else
{
WeekNumber = 52;
}
}

// 8) Find if Y M D falls in YearNumber Y+1, WeekNumber 1
if (YearNumber == Y)
{
var I:Number = 365;
if (isLeapYear)
{
var I:Number = 366;
}
if (I - DayOfYearNumber < 4 - Weekday)
{
trace('Date is within the first week of the next year.');
YearNumber = Y + 1;
WeekNumber = 1;
}
}

// 9) Find if Y M D falls in YearNumber Y, WeekNumber 1 through 53
if (YearNumber == Y)
{
trace('Date is within it\'s current year.');
var J:Number = DayOfYearNumber + (7 - Weekday) + (Jan1Weekday -1);
WeekNumber = J / 7;
if (Jan1Weekday > 4)
{
WeekNumber--;
}
}

return WeekNumber;
};```  Reply With Quote

2. Hey,

First of all excuse me for posting this reaction a month after starting the topic.

I'm currently developing a complicated Flash Visitor Counter and this function is exactly what I need.

I wonder if I can use this in such a commercial project? It's just very little part of the project but I think it's a nice feature to show the week number in some parts of the project.

In the source code and documentation I'll refer to this topic, and if you want so, to your website too.

Ruben  Reply With Quote

3. Hi Ruben,

thank you for being kind enough to ask, but I haven't put any copyright notice in the script: you are free to use as you like best!

Go for it.

Cheers  Reply With Quote

4. ## 2015?

This sems to bee a great script but what aboat 2015 it dont semms to work that year?

peter  Reply With Quote

5. What aboat 2015 it dont seems to woork that year?  Reply With Quote

6. Peterco, you could have elaborated the issue a little. Anyhow you are right, there was a glitch in my porting to ActionScript.

The function has been refactored to:
1) fix an issue with the computing of the day of the week numbers (missing floor rounding).
2) make the code compatible with AS3.

Here you go with the fixed version:

Code:
```/**
*	ISO-8601 Week Number - V 1.1
*	Processes the week number of a date,
*	just like date("W") would do in PHP.
*
*	Procedure by Rick McCarty, 1999
*	Adapted to CVI by R.Bozzolo, 2006
*	Adapted to AS2/AS3 by A.Colonna, 2008
*
*/
function calculateISO8601WeekNumber(d:Date) {
// 1) Convert date to Y M D
var Y:Number = d.getFullYear();
var M:Number = d.getMonth();
var D:Number = d.getDate();

// 2) Find out if Y is a leap year
var isLeapYear:Boolean = (Y % 4 == 0  &&  Y % 100 != 0) || Y % 400 == 0;
if (isLeapYear)
{
trace('Is a leap year');
}

// 3) Find out if Y-1 is a leap year
var lastYear:Number = Y - 1;
var lastYearIsLeap:Boolean = (lastYear % 4 == 0  &&  lastYear % 100 != 0) || lastYear % 400 == 0;
if(lastYearIsLeap)
{
trace('Is year after a leap year');
}

// 4) Find the Day of Year Number for Y M D
var month = [0,31,59,90,120,151,181,212,243,273,304,334];
var DayOfYearNumber:Number = D + month[M];
if(isLeapYear && M > 1)
DayOfYearNumber++;
trace('Day of the year: ' + DayOfYearNumber);

// 5) Find the weekday for Jan 1 (monday = 1, sunday = 7)
var YY:Number = (Y-1) % 100; // ...
var C:Number = (Y-1) - YY; // get century
var G:Number = YY + YY/4; // ...
var Jan1Weekday:Number = Math.floor(1 + (((((C / 100) % 4) * 5) + G) % 7));
trace('Day of the week for Jan 1 (1 = monday, 7 = sunday): ' + Jan1Weekday);

// 6) Find the weekday for Y M D
var H:Number = DayOfYearNumber + (Jan1Weekday - 1);
var Weekday:Number = Math.floor(1 + ((H -1) % 7));
trace('Day of the week for date (1 = monday, 7 = sunday): ' + Weekday);

var YearNumber = Y;
var WeekNumber;
// 7) Find if Y M D falls in YearNumber Y-1, WeekNumber 52 or 53
if (DayOfYearNumber <= (8-Jan1Weekday) && Jan1Weekday > 4)
{
trace('Date is within the last week of the previous year.');
YearNumber = Y - 1;
if (Jan1Weekday == 5 || (Jan1Weekday == 6 && isLeapYear))
{
WeekNumber = 53;
} else
{
WeekNumber = 52;
}
}

// 8) Find if Y M D falls in YearNumber Y+1, WeekNumber 1
if (YearNumber == Y)
{
var I:Number = 365;
if (isLeapYear)
{
var I:Number = 366;
}
if (I - DayOfYearNumber < 4 - Weekday)
{
trace('Date is within the first week of the next year.');
YearNumber = Y + 1;
WeekNumber = 1;
}
}

// 9) Find if Y M D falls in YearNumber Y, WeekNumber 1 through 53
if (YearNumber == Y)
{
trace('Date is within it\'s current year.');
var J:Number = DayOfYearNumber + (7 - Weekday) + (Jan1Weekday -1);
WeekNumber = J / 7;
if (Jan1Weekday > 4)
{
WeekNumber--;
}
}

return WeekNumber;
};```
You can easily test the code with the following lines (which should work both in AS2 and AS3):
Code:
```var dates:Array = [new Date(2009, 0, 1, 12, 1, 1, 0),
new Date(2010, 0, 1, 12, 1, 1, 0),
new Date(2011, 0, 1, 12, 1, 1, 0),
new Date(2012, 0, 1, 12, 1, 1, 0),
new Date(2013, 0, 1, 12, 1, 1, 0),
new Date(2014, 0, 1, 12, 1, 1, 0),
new Date(2015, 0, 1, 12, 1, 1, 0)];

for(var i:int = 0; i < dates.length; i++) {
trace('Computing week number for date: ' + dates[i].toString());
trace('Week number is: ' + calculateISO8601WeekNumber(dates[i]));
trace('==============================');
}```
This should give you the following output, which can be easily verified.
The week index has base 1.

Code:
```Computing week number for date: Thu Jan 1 12:01:01 GMT+0100 2009
Is year after a leap year
Day of the year: 1
Day of the week for Jan 1 (1 = monday, 7 = sunday): 4
Day of the week for date (1 = monday, 7 = sunday): 4
Date is within it's current year.
Week number is: 1
==============================
Computing week number for date: Fri Jan 1 12:01:01 GMT+0100 2010
Day of the year: 1
Day of the week for Jan 1 (1 = monday, 7 = sunday): 5
Day of the week for date (1 = monday, 7 = sunday): 5
Date is within the last week of the previous year.
Week number is: 53
==============================
Computing week number for date: Sat Jan 1 12:01:01 GMT+0100 2011
Day of the year: 1
Day of the week for Jan 1 (1 = monday, 7 = sunday): 6
Day of the week for date (1 = monday, 7 = sunday): 6
Date is within the last week of the previous year.
Week number is: 52
==============================
Computing week number for date: Sun Jan 1 12:01:01 GMT+0100 2012
Is a leap year
Day of the year: 1
Day of the week for Jan 1 (1 = monday, 7 = sunday): 7
Day of the week for date (1 = monday, 7 = sunday): 7
Date is within the last week of the previous year.
Week number is: 52
==============================
Computing week number for date: Tue Jan 1 12:01:01 GMT+0100 2013
Is year after a leap year
Day of the year: 1
Day of the week for Jan 1 (1 = monday, 7 = sunday): 2
Day of the week for date (1 = monday, 7 = sunday): 2
Date is within it's current year.
Week number is: 1
==============================
Computing week number for date: Wed Jan 1 12:01:01 GMT+0100 2014
Day of the year: 1
Day of the week for Jan 1 (1 = monday, 7 = sunday): 3
Day of the week for date (1 = monday, 7 = sunday): 3
Date is within it's current year.
Week number is: 1
==============================
Computing week number for date: Thu Jan 1 12:01:01 GMT+0100 2015
Day of the year: 1
Day of the week for Jan 1 (1 = monday, 7 = sunday): 4
Day of the week for date (1 = monday, 7 = sunday): 4
Date is within it's current year.
Week number is: 1
==============================```  Reply With Quote

7. Yes now it seems to work just fine.

Peter  Reply With Quote

8. Thank you Peter...  Reply With Quote

9. Thank you!

I found some errors in the code:
Code:
```var I:Number = 365;
if (isLeapYear)
{
var I:Number = 366;
}```
It should be:
Code:
```var I:Number = 365;
if (isLeapYear)
{
I = 366;
}```
I have added some declarations of some variables to avoid varnings.

My version:

Code:
```/**
*	ISO-8601 Week Number - V 1.1
*	Processes the week number of a date,
*	just like date("W") would do in PHP.
*
*	Procedure by Rick McCarty, 1999
*	Adapted to CVI by R.Bozzolo, 2006
*	Adapted to AS2/AS3 by A.Colonna, 2008
*
*/
function calculateISO8601WeekNumber(d:Date) {
// 1) Convert date to Y M D
var Y:Number = d.getFullYear();
var M:Number = d.getMonth();
var D:Number = d.getDate();

// 2) Find out if Y is a leap year
var isLeapYear:Boolean = (Y % 4 == 0  &&  Y % 100 != 0) || Y % 400 == 0;
if (isLeapYear)
{
trace('Is a leap year');
}

// 3) Find out if Y-1 is a leap year
var lastYear:Number = Y - 1;
var lastYearIsLeap:Boolean = (lastYear % 4 == 0  &&  lastYear % 100 != 0) || lastYear % 400 == 0;
if(lastYearIsLeap)
{
trace('Is year after a leap year');
}

// 4) Find the Day of Year Number for Y M D
var month:Array = [0,31,59,90,120,151,181,212,243,273,304,334];
var DayOfYearNumber:Number = D + month[M];
if(isLeapYear && M > 1)
DayOfYearNumber++;
trace('Day of the year: ' + DayOfYearNumber);

// 5) Find the weekday for Jan 1 (monday = 1, sunday = 7)
var YY:Number = (Y-1) % 100; // ...
var C:Number = (Y-1) - YY; // get century
var G:Number = YY + YY/4; // ...
var Jan1Weekday:Number = Math.floor(1 + (((((C / 100) % 4) * 5) + G) % 7));
trace('Day of the week for Jan 1 (1 = monday, 7 = sunday): ' + Jan1Weekday);

// 6) Find the weekday for Y M D
var H:Number = DayOfYearNumber + (Jan1Weekday - 1);
var Weekday:Number = Math.floor(1 + ((H -1) % 7));
trace('Day of the week for date (1 = monday, 7 = sunday): ' + Weekday);

var YearNumber:Number = Y;
var WeekNumber:Number;
// 7) Find if Y M D falls in YearNumber Y-1, WeekNumber 52 or 53
if (DayOfYearNumber <= (8-Jan1Weekday) && Jan1Weekday > 4)
{
trace('Date is within the last week of the previous year.');
YearNumber = Y - 1;
if (Jan1Weekday == 5 || (Jan1Weekday == 6 && isLeapYear))
{
WeekNumber = 53;
} else
{
WeekNumber = 52;
}
}

// 8) Find if Y M D falls in YearNumber Y+1, WeekNumber 1
if (YearNumber == Y)
{
var I:Number = 365;
if (isLeapYear)
{
I = 366;
}
if (I - DayOfYearNumber < 4 - Weekday)
{
trace('Date is within the first week of the next year.');
YearNumber = Y + 1;
WeekNumber = 1;
}
}

// 9) Find if Y M D falls in YearNumber Y, WeekNumber 1 through 53
if (YearNumber == Y)
{
trace('Date is within it\'s current year.');
var J:Number = DayOfYearNumber + (7 - Weekday) + (Jan1Weekday -1);
WeekNumber = J / 7;
if (Jan1Weekday > 4)
{
WeekNumber--;
}
}

return WeekNumber;
};```  Reply With Quote

10. ## another way :

another way :

Code:
```public function WeekNumber(adate:Date):int{
//
// 1 - CALC JULIAN DATE NUMBER (note: nothing to do with the Julian calendar)
//
var wmonth:int = adate.getMonth() + 1;
//
var wa:int = Math.floor((14 - wmonth) / 12);
var wy:int = wfullyear + 4800 - wa;
var wm:int = wmonth + 12 * wa - 3;
//
// wJDN is the Julian Day Number
//
var wJDN:int = wdate + Math.floor(((153 * wm) + 2) / 5) + wy * 365
+ Math.floor(wy / 4)
- Math.floor(wy / 100)
+ Math.floor(wy / 400) - 32045;
//
// 2 - CALC WEEK NB
//
var d4:int = (((wJDN + 31741 - (wJDN % 7)) % 146097) % 36524) % 1461;
var L:int = Math.floor(d4 / 1460);
var d1:int = ((d4 - L) % 365) + L;
var wweekNb:int = Math.floor(d1 / 7) + 1;
return wweekNb;
};```  Reply With Quote

11. Originally Posted by Michael REMY another way :

Code:
```public function WeekNumber(adate:Date):int{
//
// 1 - CALC JULIAN DATE NUMBER (note: nothing to do with the Julian calendar)
//
var wmonth:int = adate.getMonth() + 1;
//
var wa:int = Math.floor((14 - wmonth) / 12);
var wy:int = wfullyear + 4800 - wa;
var wm:int = wmonth + 12 * wa - 3;
//
// wJDN is the Julian Day Number
//
var wJDN:int = wdate + Math.floor(((153 * wm) + 2) / 5) + wy * 365
+ Math.floor(wy / 4)
- Math.floor(wy / 100)
+ Math.floor(wy / 400) - 32045;
//
// 2 - CALC WEEK NB
//
var d4:int = (((wJDN + 31741 - (wJDN % 7)) % 146097) % 36524) % 1461;
var L:int = Math.floor(d4 / 1460);
var d1:int = ((d4 - L) % 365) + L;
var wweekNb:int = Math.floor(d1 / 7) + 1;
return wweekNb;
};```
very useful! thanks...  Reply With Quote

12. Hi,

I just realized that the code starts counting from the week of Monday, for example, when I tested the date "2/1/2011" he returned weeks 0.

As workaround, i change the last "if" of your code, and he becomes something like that:

/ / 9) Find if YMD falls in YearNumber Y WeekNumber 1 through 53
if (Y == YearNumber)
{
var J:Number = DayOfYearNumber + (7 - Weekday) + (Jan1Weekday -1);
WeekNumber = J / 7;
if (Jan1Weekday> 4)
{
WeekNumber -;
}
if (d.getDay () == 0)
{
WeekNumber + +;
}
}

Thanks.  Reply With Quote

13. ## weeknumber by date

I am new to this forum but perhaps this is a more simple way to get week numbers?

First I set firstweek to 4th of january which is always in the first week.

Then I set firstweek to the last hour, minute and millisecond of sunday in the first week

As long as current is bigger than firstweek I add 7 days while at the same time counting up weeknumber

I seems to work - and as the stepping is done with the date object i guess that leap years is catered for also

feel free to comment

Code:
```function getweeknumber(current:Date):Number {
var weeknumber=1
var firstweek=new Date()
firstweek.setFullYear(current.getFullYear(),0,4)
var sunday=4-firstweek.getDay()+7
firstweek.setDate(sunday)
firstweek.setHours(23,59,59,999)
while(current>firstweek) {
firstweek.setDate(firstweek.date+7)
weeknumber++
}
return(weeknumber)
}```  Reply With Quote

14. Hi, i'm new to this forum but perhaps here is an easier way to get weeknumbers.

First I set firstweek to 4.th of january which is always in week 1
Then I set firstweek to the last hour,minute and millisecond of sunday in week 1
While current is bigger than firstweek I add 7 days while counting up weeknumber

It seems to work and as I am using the date object for stepping I guess it catches leap years also

Code:
```function getweeknumber(current:Date):Number {
var weeknumber=1
var firstweek=new Date()
firstweek.setFullYear(current.getFullYear(),0,4)
var sunday=4-firstweek.getDay()+7
firstweek.setDate(sunday)
firstweek.setHours(23,59,59,999)
while(current>firstweek) {
firstweek.setDate(firstweek.date+7)
weeknumber++
}
return(weeknumber)
}```  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

 » Home » Movies » Tutorials » Submissions » Board » Links » Reviews » Feedback » Gallery » Fonts » The Lounge » Sound Loops » Sound FX » About FK » Sitemap 