• Introducing Dark Mode! Switch by clicking on the lightbulb icon next to Search or by clicking on Default style at the bottom left of the page!

How to retrieve Week N umbers



I'm a junior developer.
The client is asking me too make a report where they are able to select items with a certain quantity per week.
My question is does anybody know how I can get the Week Numbers
Thank you very much for your effort.

OW XE B7333
Win2000 Fat Client


Active Member
Do you mean you want to get the number of the week within the year?

ie that 10/01/01 is week 2 2001???

if so, can you not divide the day of the year(from the last 3 characters of the julian date for today) by 7? You may have to make some adjustment depending on whether the year starts on a monday, but you can probably work around this.

Any help? please expand your question if not

Tom Brown


Active Member
I have used this code in the past:

NAMED ER: Get the WeekNumber
VA evt_Jaar_YR = date_year([BF InputDate])
VA evt_Jaar_YR = substr([VA evt_Jaar_YR],2,2 )
Format Date From Day Month Year
VA evt_Jaar_YR -> mnYear
"1" -> mnMonth
"1" -> mnDay
VA evt_1Jan_DD <- jdDate
Retrieve Day of Week
VA evt_1Jan_DD -> jdDate
VA evt_DOW <- mnDayofWeek
UNDEFINED X szDayofWeekDesc
If VA evt_DOW is less than "4"
BF mnWeekNumber = floor(days_between([VA evt_1Jan_DD],[BF InputDate])/7)+2
BF mnWeekNumber = floor(days_between([VA evt_1Jan_DD],[BF InputDate])/7)+1
End If


Walter Herdes.

B7332 / XE / NT4 / SQL 7 / VisualStudio 6 SP5


Hi Wherdes

I've tried your coding,it works fine as long there are 52 weeks in a year.
But when there's a year with 53 weeks it will go blank.
Can you point me the right direction to correct this little flaw.
Thank you very much.

OW XE B7333
Win2000 FatClient



Active Member
You are right, I've corrected the code. This must work in case year has 53 weeks.

Walter Herdes

NAMED ER: Get Number Of the Week
0001 If BF cMode is not equal to "1"
0002 BF mnCenturyYear = date_year([BF jdDate])
0003 End If
0004 VA evt_mnDtMatchCkOrItemYr_DTMY = mod([BF mnCenturyYear],100)
0005 VA evt_mnDtMatchCkOrItemCtry_DTMx = ([BF mnCenturyYear]-[VA

0006 User Date, Format
"1" -> mnMonth
"1" -> mnDay
VA evt_mnDtMatchCkOrItemYr_DTMY -> mnYear
VA evt_mnDtMatchCkOrItemCtry_DTMx -> mnCentury
VA evt_jdDate_DTE <- jdReturnDate
"<Blank>" -> cProcessingFlag
0007 Get The Day Of The Week - 0 = Sunday, 6 = Saturday
VA evt_jdDate_DTE -> jdDate
VA evt_mnDayOfWeekAJanuary1st_DOW <- mnDayofWeekA
UNDEFINED <- cErrorCode
0008 If VA evt_mnDayOfWeekAJanuary1st_DOW is equal to <Zero>
0009 VA evt_mnDayOfWeekA_DOW = "7"
0010 Else
0011 VA evt_mnDayOfWeekA_DOW = VA evt_mnDayOfWeekAJanuary1st_DOW
0012 End If
0013 If VA evt_mnDayOfWeekA_DOW is greater than "4"
0014 VA evt_jdDate_DTE = add_days([VA evt_jdDate_DTE],8-[VA evt_mnDayOfWeekA_DOW])
0015 Else
0016 If VA evt_mnDayOfWeekA_DOW is not equal to "1"
0017 VA evt_jdDate_DTE = add_days([VA evt_jdDate_DTE],1-[VA evt_mnDayOfWeekA_DOW])
0018 End If
0019 End If
0020 If BF cMode is not equal to "1"
0021 VA evt_mnElapsedDays_EDAYS = days_between([VA evt_jdDate_DTE],[BF jdDate])
0022 BF mnWeekNumber = floor([VA evt_mnElapsedDays_EDAYS]/7+1)
0023 BF mnDayofWeekA = mod([VA evt_mnElapsedDays_EDAYS]-[VA

0024 If BF mnWeekNumber is equal to "53"
0025 VA evt_mnElapsedDays_EDAYS = mod([VA evt_mnDtMatchCkOrItemYr_DTMY],4)
0026 If VA evt_mnElapsedDays_EDAYS is not equal to <Zero>
0027 VA evt_mnDayOfWeekA_DOW = [VA evt_mnDayOfWeekA_DOW]+1
0028 Else
0029 VA evt_mnDayOfWeekA_DOW = [VA evt_mnDayOfWeekA_DOW]+2
0030 End If
0031 If VA evt_mnDayOfWeekA_DOW is greater than "7"
0032 VA evt_mnDayOfWeekA_DOW = [VA evt_mnDayOfWeekA_DOW]-7
0033 End If
0034 If VA evt_mnDayOfWeekA_DOW is less than "5"
0035 BF mnCenturyYear = [BF mnCenturyYear]+1
0036 BF mnWeekNumber = "1"
0037 End If
0038 Else
0039 If BF mnWeekNumber is equal to <Zero>
0040 BF mnCenturyYear = [BF mnCenturyYear]-1
0041 VA evt_mnDtMatchCkOrItemYr_DTMY = mod([BF mnCenturyYear],100)
0042 VA evt_mnDtMatchCkOrItemCtry_DTMx = ([BF mnCenturyYear]-[VA

0043 VA evt_mnElapsedDays_EDAYS = mod([VA evt_mnDtMatchCkOrItemYr_DTMY],4)
0044 If VA evt_mnElapsedDays_EDAYS is not equal to <Zero>
0045 VA evt_mnDayOfWeekA_DOW = [VA evt_mnDayOfWeekA_DOW]-1
0046 Else
0047 VA evt_mnElapsedDays_EDAYS = mod([VA evt_mnDtMatchCkOrItemCtry_DTMx],4)
0048 If VA evt_mnElapsedDays_EDAYS is not equal to <Zero>
0049 VA evt_mnDayOfWeekA_DOW = [VA evt_mnDayOfWeekA_DOW]-1
0050 Else
0051 VA evt_mnDayOfWeekA_DOW = [VA evt_mnDayOfWeekA_DOW]-2
0052 End If
0053 End If
0054 If VA evt_mnDayOfWeekA_DOW is less than "1"
0055 VA evt_mnDayOfWeekA_DOW = [VA evt_mnDayOfWeekA_DOW]+7
0056 End If
0057 If VA evt_mnDayOfWeekA_DOW is greater than "4"
0058 BF mnWeekNumber = "52"
0059 Else
0060 BF mnWeekNumber = "53"
0061 End If
0062 End If
0063 End If
0064 Else
0065 If BF mnDayofWeekA is equal to <Zero>
0066 VA evt_mnElapsedDays_EDAYS = [BF mnWeekNumber]*7
0067 Else
0068 VA evt_mnElapsedDays_EDAYS = ([BF mnWeekNumber]-1)*7+[BF mnDayofWeekA]
0069 End If
0070 BF jdDate = add_days([VA evt_jdDate_DTE],[VA evt_mnElapsedDays_EDAYS]-1)
0071 End If

B7332 / XE / NT4 / SQL 7 / VisualStudio 6 SP5


Hi Wherdes

I've tried to recreate the BSFN you send me the 2nd time but I've encountered a problem where you are saying
VA evt_mnDTMatchCkOrItemCtry_DTMX=([BF mnCenturyYear]-[VA evt_mnDtMatchCkOrItemYr_DTMY]/100.
JD gives a error message Expression's data Type (Number) and Assignment's Data Type (Date)are incompatible.
Is it possible for you to tell me what I've done wrong here.
I'm sorry for disturbing you with these questions.

Win 2000 OWxe fatclient



Legendary Poster
Hi David,

Excuse me that I interrupt the conversation on this thread and I try to answer your question instead of the author Walter.

Studying Walter's code I suppose that "VA evt_mnDTMatchCkOrItemCtry_DTMx" is not based on DTMX Data Item which has Date data type but it is also based on DTMY or on any other Math Numeric Data Item.

My arguments are:
1.) The "mn" prefix in the variable name says that this is a Math Numeric variable.
2.) The variable is used everywhere in Walter's code as Numeric and never used as Date.

Hope could help,


B7332 SP11, ESU 4116422, Intel NT4, SQL 7 SP1
(working with B7321, B7331, XE too)


Active Member
I am not understanding what Modes could be passed in? So what variables are required to pass into the NER? Can we get just a quick explanation of what it will do?



Legendary Poster
B0130151 gives you weeks and months of the year


N0501033 gives you weeks and days between 2 dates

so 1st date = 1/1/year you are in
2nd date = date with the week you want

today is the 37.42th week of the year

If I am stuck like this I go and read F9862 (business function list)
Place Week into SIMD and click find.
I then use Object Browser from Patwel to see if it works as I want



Legendary Poster
Actually week number is not quite as straight forward as it first may appear. There is actually several ways to calculate it including two ANSI C standards and an ISO (ISO 8601) standard.

A few years back we created an application that needed this information calculated consistently and very fast (no table i/o or anything). Attached is a function (see AcmeGetWeekInYear) I wrote that will calculate the week number for a given year using the three different standards I just mentioned. Here are the .h notes for the methods.

<font class="small">Code:</font><hr /><pre>
/********** Week In Year Methods **********

Week start=Monday, week end=Sunday
Week = 1-53
Assigns week to year and calcs week num based on which year has the most days

Week start=Monday, week end=Sunday
Week = 0-53
The first Monday of January is the first day of week 1; days in the new year before this are in week 0

Week start=Sunday, week end=Saturday
Week = 0-53
The first Sunday of January is the first day of week 1; days in the new year before this are in week 0

</pre><hr />



Legendary Poster
I've been thinking about this..

Do you actually need to know the week?

Can't you just start at your start date and add 7 days to using the add days to date system function?
You now have a start and a to date...compare the 'orders' transaction date you read through to see if it's in that range

Have a counter and loop until you incrementing date exceeds the end date you chose?

Also is this financial year week or calendar year week?