Wiki Home

Calculationof Week Numbers - Modified ISO

(Updated: 2005.01.07 11:43:46 AM)
Namespace: Wiki
I need to calculate week numbers. We have a production week that starts on Sunday. This means that we use ISO 8601 week numbers offset by one day. In 2004 and 2005, this means that Sunday 26 Dec 2004 is the first day of week 53, and Saturday 1 Jan 2005 is the last day of week 53. Sunday 2 Jan 2005 is the first day of week 1 2005 and Saturday 8 Jan 2005 is the last day of week 1 2005.

I have used Ramani's week number function for a number of years, but this, if adjusted to give all the right week numbers for 2005, does not calculate the week 53 correctly for 2004, making all of the week 53 as 52. It also does not put the 1 Jan 2005 in week 53, where I want it, but in week 1.

This is the function that works for 2 Jan 2005 to 31 Dec 2005.

FUNCTION getweekno
   PARAMETER mydate
   mdays = mydate - CTOD("01/01/"+ALLT(STR(YEAR(mydate))))
   year1dt = mydate - mdays
   weekstart = DOW(year1dt)
   mdays = mdays + weekstart - 1
   nweek = INT(mdays/7)+1
   RETURN nweek


Anyone got any ideas?

(More dates for info:)

28 Dec 2003 - 3 Jan 2004 Wk 1 2004
19 Dec 2004 - 25 Dec 2004 Wk 52 2004
26 Dec 2004 - 1 Jan 2005 Wk 53 2004

2 Jan 2005 - 8 Jan 2005 Wk 1 2005
9 Jan 2005 - 15 Jan 2005 Wk 2 2005
....
18 Dec 2005 - 31 Dec 2005 Wk 52 2005


This solution was proposed by theRambler on TekTips, and it works perfectly for my one day
offset ISO 8601 week numbers. It uses SYS(11) the Julian day number.

*!*****************************************************************************
*!
*!       Function: GETWEEKNO()
*!
*!*****************************************************************************
FUNCTION getweekno
PARAMETERS mydate
jday = VAL(SYS(11,mydate))+1
d4 = mod(mod(mod((Jday+31741 - mod(Jday,7)),146097),36524),1461)
L = INT(d4/1460)
d1 = mod(d4-L,365) + L
return(INT(d1/7)+1)
ENDFUNC