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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Week Of Month, First Day & NextSunday functions

Usefull Functions & Procedures

Week Of Month, First Day & NextSunday functions

by  GriffMG  Posted    (Edited  )
** 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)

Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top