** simple functions for WeekOfMonth
** This bit helps you test it!
clear
set date british
m.date = date()
do while lastkey() <> 27
@ 10,10 say "Date" get m.date
read
clear gets
@ 11,10 say FirstDay(m.date)
@ 12,10 say NextSunDay(FirstDay(m.date))
@ 13,10 say WeekOfMonth(m.date)
enddo
** this is the actual function
function WeekOfMonth
parameter m.date
private m.date,i,m.factor
** the week is...
** first get the day of the first sunday in the month
** by getting the first day, and then getting the next Sunday
** after that
i = Day(NextSunDay(FirstDay(m.date)))
** if the day isn't before that first sunday....
if Day(m.date) > i
if i > 1
m.factor = 2
else
m.factor = 1
endif
** calculate the differnce between the days (of your date and the
** first sunday) divide that by the number of days in a week
** (taking just the integer bit) and add the week factor - because
** the first week has been excluded by the if, and we want the
** balance to start at 2 for weeks after that
i = int((Day(m.date)-i)/7)+m.factor
else
i = 1
endif
return(i)
** this always gives you the first day of any month
function FirstDay
parameter m.date
private m.date
m.date = m.date - Day(m.date) + 1
return(m.date)
** this returns the next Sunday...
function NextSunDay
parameter m.date
private m.date,i
i = 8 - dow(m.date) && 1=Sunday
if i < 7
m.date = m.date + i
endif
return(m.date)
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.