* Purpose of this function to return week number based on Year/Month/Week FUNCTION WeekOfDate LPARAMETERS tnYear, tnMonth, tnWeek, cOption *-- Option cOption has empty value if the week number is to *-- be determined by the first Monday of the month. Local mon_1, date_1, day_1 Local i, firstdate, first_mon, nWeekNumber Local cLastDayOfMonth, lIsLeapYear Local nDayOfWeek if pcount() < 4 cOption = " " endif *--- First of the year: date_1 = CTOD(STR(tnmonth,2)+'/01/'+SUBSTR(STR(tnyear,4),3,2)) *--- Day of the week of the first date. day_1 = DOW(date_1) do case case !( cOption $ "FIL") && cOption has a number. *--- Day of the week nDayOfWeek = int(val( cOption )) *--- Make sure that nDayOfWeek is within valid range: if nDayOfWeek < 1 or nDayOfWeek > 7 nDayOfWeek = 2 endif *--- Scan through 7 days to find the first date matching Day of Week *--- criterion. For example, if passed nDayOfWeek is 4, find first *--- Wednesday of the month. for i = 1 to 7 if day_1 = nDayOfWeek exit endif day_1 = iif( day_1 < 7, day_1 + 1, 1 ) date_1 = date_1 + 1 next *--- Increment this date by the Week Number. That is, find 2nd ccurance *--- of Wednesday, Thursday, etc. date_1 = date_1 + (tnWeek - 1) * 7 case cOption = "I" *--- First weekday of the month. if dow( date_1 ) = 1 && Sunday date_1 = date_1 + 1 && Go to next Monday endif if dow( date_1 ) = 7 && Saturday date_1 = date_1 + 2 && Go to next Monday endif *--- Otherwise, use date "date_1" case cOption = "L" *--- Last day of the month lIsLeapYear = (tnYear % 4 = 0) and ((tnYear % 100 # 0) OR (tnYear % 400 = 0)) *--- Last day of the month as a character cLastDayOfMonth = substr( "31" + iif(lIsLeapYear,"29","28") + ; "31303130313130313031", (tnmonth*2-1), 2 ) *--- Last date of the month date_1 = CTOD(STR(tnmonth,2)+'/' + cLastDayOfMonth + '/'+SUBSTR(STR(tnyear,4),3,2)) * NOTE: the DO CASE does not have a case for cOption = "F" since it is the same as the first day of the month "date_1" endcase *--- Find the closest Monday previous to the determined date. *--- Reason: GPM needs Monday date to determine the week number. for i = 1 to 7 if dow( date_1 ) = 2 && Monday exit endif day_1 = iif( day_1 > 1, day_1 - 1, 7 ) date_1 = date_1 - 1 next firstdate = CTOD('01/01/'+SUBSTR(STR(YEAR(date_1),4),3,2)) IF DOW(firstdate) > 2 first_mon = firstdate + 9 - DOW(firstdate) ELSE first_mon = firstdate + 2 - DOW(firstdate) ENDIF nWeekNumber = int(( date_1 - first_mon) / 7 + 1) RETURN (nWeekNumber)>Hi Dmitry,