KB#00865-User Defined Functions to return Date of Week from Week Number

Title:

User Defined Functions to return Date of Week from Week Number entered and Week Number from Date Entered

Description:

The following user defined function returns the monday date (as yyyymmdd) for any given week in the year (as yyyyww).

For this exercise the week starts on Monday and ends on Sunday. Week one of the year is the week with the first Monday, eg week 200001 should return 20000103, week 200028 should return 20000710

def fnMONDAY\$(WEEK\$)
let YR=num(WEEK\$(1,4)),WK=num(WEEK\$(5)),MO=1,DA=1
while mod(jul(YR,MO,DA),7)>0; let DA=DA+1; wend
let J=jul(YR,MO,DA)+(7*(WK-1))
return date(J:"%Yl%Mz%Dz")
fnend

***********************************************

The following user defined function calculates any week number a date entered falls in (a date to week conversion.) It also back dates prior to the 1st week of the year into the previous year (eg, 1/1/2000 and 1/2/2000 belong to week 199952, since 200001 doesn't start until 1/3/2000

def fnWEEK\$(DATE\$)
let YR=num(DATE\$(1,4)),MO=num(DATE\$(5,2)),DA=num(DATE\$(7,2))
let J1=jul(YR,MO,DA),J2=jul(YR,1,1)
while mod(J2,7)>0; let J2=J2+1; wend
while mod(J1,7)>0; let J1=J1-1; wend
let WEEK=int((J1-J2)/7)+1
if WEEK<1 then let WEEK=WEEK+52,YR=YR-1
return str(YR:"0000")+str(WEEK:"00")
fnend