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

how to get week of the month?

Status
Not open for further replies.

FoxKid

MIS
Jun 8, 2003
92
IN
Hi all,
There is an option to get week of the year i.e. week(), but is there any command or function to get week of the month?

Thanks and Regards
 
R17, you have not understand my question. I want week of month not day of week.

Thanks and Regards
 
FoxKid,

So, to be clear - you want to know if the 13th of July is in the second or third week of that month?

If we can assume that the 1st of any month is in week 1, can you tell us on which day you want the next week to start (i.e. Monday or Sunday). That shouldn't be too hard!

~ 8 )

Regards

Griff
Keep [Smile]ing
 
Code:
** simple functions for WeekOfMonth
** This bit helps you test it!

clear
set date british
m.date = date()
do while lastkey() <> 27
	@ 10,10 say &quot;Date&quot; 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
	** 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
		** 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 two days - because
		** the first week has been excluded by the if, and we want the
		** balance to start at 2
		i = int((Day(m.date)-i)/7)+2
	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 = ctod(&quot;01/&quot;+alltrim(str(month(m.date)))+&quot;/&quot;+alltrim(str(year(m.date))))
	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)

Does this help?

Regards

Griff
Keep [Smile]ing
 
GriffMG,

Nice bit of code that. However, I seem to find a problem with it. If you enter 22/06/2003 (June 22nd, 2003) it will tell you that it is week 5, however on my calendar it shows as the start of week 4. Perhaps it cannot handle a month that starts on Sunday?

Slighthaze = NULL
craig1442@mchsi.com
&quot;Whom computers would destroy, they must first drive mad.&quot; - Anon​
 
Oooh, I will check that (maths awry this morning!)



Regards

Griff
Keep [Smile]ing
 
Sorry, this is a bit of a kludge:

Code:
** simple functions for WeekOfMonth
** This bit helps you test it!

clear
set date british
m.date = date()
do while lastkey() <> 27
	@ 10,10 say &quot;Date&quot; 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 = ctod(&quot;01/&quot;+alltrim(str(month(m.date)))+&quot;/&quot;+alltrim(str(year(m.date))))
	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)


Regards

Griff
Keep [Smile]ing
 
Griff, Thankyou very much for your codes but it will not help me out as I was in search of any function or single row command to get it.
Now, I have written codes for it and used it and solves my problem.
Your codes will definately going to help me in future. Thanks again for your reply.
Actually, I just wanted that if the date is between 1 to 7 it should come in 1st week then if between 8 to 14 it should come in 2nd week and so on....

Thanks and regards
 
Ah, gotcha - that's really easy maths:

Code:
x = int((day(m.date)-1)/7)+1

Now I've done it, I'll make my original solution into a FAQ

Good Luck

Regards

Griff
Keep [Smile]ing
 
Or to do it in one less math operation...

lddate = {^2003/06/22}
lnWeek = INT((DAY(lddate) + 6)/7) && Adds then divides, no subtraction required

...I'm just being anal, but with all the fancy stuff GriffMG did, I feel the need [wink]

Slighthaze = NULL
craig1442@mchsi.com
&quot;Whom computers would destroy, they must first drive mad.&quot; - Anon​
 
GriffMG,

Another way to get the first day of the month:

m.date = m.date - Day(m.date) + 1

Slighthaze = NULL
craig1442@mchsi.com
&quot;Whom computers would destroy, they must first drive mad.&quot; - Anon​
 
Slighthaze,

THAT is a very elegant bit of code - I like that, purely mathematical solutions appeal to me - gets rid of all those 'if's and whotnot!

I'll mod the FAQ

Regards

Griff
Keep [Smile]ing
 
The best way to go about what FoxKid wanted does not seem to be:

lddate = {^2003/06/22}
lnWeek = INT((DAY(lddate) + 6)/7)

but rather this:

lddate = {^2003/06/22}
lnWeek = CEILING(DAY(lddate)/7)

...why this did not occur to me earlier I do not know, but I have recently needed just such a line of code and upon reviewing this thread I came up with the above code instead.

Slighthaze = NULL
craig1442@mchsi.com
&quot;Whom computers would destroy, they must first drive mad.&quot; - Anon​
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top