wadesnj
Programmer
- Mar 24, 2001
- 36
- 0
- 0
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 2004 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 - 8
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
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 2004 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 - 8
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