Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Week Number Calculation - modified ISO 1

Status
Not open for further replies.

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


 
Hi,

I found this function here
Code:
d4 = (J+31741 - (J mod 7)) mod 146097 mod 36524 mod 1461
L = d4/1460
d1 = ((d4-L) mod 365) + L
WeekNumber = d1/7+1
and adapted it to work with FPD syntax and to suit your needs.
Code:
FUNCTION WeekNumber
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)
Try it...
 
Many thanks for your excellent function TheRambler. I have tested it for all the relevant dates in our production calendar for 2004, 2005, and 2006 and all weeknumbers are correct!

I spent ages trying to find a modified ISO function that worked, and didn't find your source! Once again, thanks for your help.

Best regards

Steve Wade
 
Thanks for the star!
Glad I could help.
I got that source from the History forum (forum1391).
Funny someone thought that forum was not related to IT. Fortunatelly now everything is fine.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top