Number of Sundays, Mondays, etc. in a month

Thread: Number of Sundays, Mondays, etc. in a month

1. KPW Guest

Number of Sundays, Mondays, etc. in a month

I wrote a function to determine the number of Sundays, Mondays, etc., in a month. I was wondering if VB and VBScript already have a function to do this, or if anyone else has done something similar. If you have done something similar, does it look any better than my example below? (The example is actually VB code rather than VBScript; some minor changes would of course need to be made for VBScript.)<BR><BR>Function GetDayRecurrences(ByVal iThisMonth as Integer, ByVal iThisYear as Integer, ByVal iThisWeekday as Integer) as Integer<BR><BR> &#039 --- iThisMonth is the month you are interested in ---<BR> &#039 --- iThisYear is the year you are interested in ---<BR> &#039 --- iThisWeekday is the weekday you are looking for ---<BR><BR>Â*Â*Dim iRecurrences as integer<BR>Â*Â*Dim dDate as Date<BR><BR>Â*Â*For i = 1 to 31<BR>Â*Â*Â*Â*If IsDate(CStr(iThisMonth) & "/" & i & "/" & CStr(iThisYear)) then<BR>Â*Â*Â*Â*Â*Â*dDate = CStr(iThisMonth) & "/" & i & "/" & CStr(iThisYear)<BR>Â*Â*Â*Â*Â*Â*If Weekday(dDate) = iThisWeekday then<BR>Â*Â*Â*Â*Â*Â*Â*Â*iRecurrences = iRecurrences + 1<BR>Â*Â*Â*Â*Â*Â*End If<BR>Â*Â*Â*Â*End If<BR>Â*Â*Next i<BR>Â*Â*GetDayRecurrences = iRecurrences<BR>End Function

2. KPW Guest

Sorry, I don&#039t want to sound needy, but I really would like your opinions on my previous post.<BR><BR>I had some additional thoughts on this. After I posted this, I wondered if I was being stupid. I mean, aren&#039t there pretty much four occurrences of each weekday in a month? But no, if you look at January 2000 you will see that there are 5 Saturdays, Sundays, and Mondays, but only 4 of all the other days. If you look at February 2000, there are 5 Tuesdays but only because of leap year; in a non-leap year, there would be 4 of each weekday.<BR><BR>Anyhow, my first thought on how to tackle this was to figure out which day of the week the month started on, then figure out which day of the week it ended on, then go from there. Of course, I couldn&#039t figure out how that would work, so I came up with the code I posted.<BR><BR>Thanks!

3. Senior Member
Join Date
Dec 1969
Posts
168

Here are two functions that will return the weekday that a given month/year starts with / ends with ( in numerical format.. ie 1 = Sunday, 2 = Monday,..etc)<BR><BR>Function firstdayofmonth(m,y)<BR> If Not m &#060; 1 And Not m &#062; 12 Then<BR> firstdayofmonth = WeekDay(DateSerial(y, m, 1))<BR> Else<BR> Response.Write("Error: Month (" & m & ") out of range")<BR> End If<BR>End Function<BR><BR>Function lastdayofmonth(m,y)<BR> If Not m &#060; 1 And Not m &#062; 12 Then<BR> Dim d, tmp<BR> d = 31<BR> Do<BR> If IsDate(DateSerial(y, m, d)) Then tmp = WeekDay(DateSerial(y, m, d))<BR> d = d - 1<BR> Loop While (tmp = 0 And d &#062;= 28)<BR> lastdayofmonth = tmp<BR> Else<BR> Response.Write("Error: Month (" & m & ") out of range")<BR> End If<BR>End Function<BR><BR>Hope this helps...Cheers!

4. KPW Guest

No, not really what I wanted

Thanks, but I know how to find out which day a month starts or ends on. I think the post you responded to was a bit unclear about what I wanted. I would like to get opinions on my original post, which is titled "Number of Sundays, Mondays, etc. in a month." Basically I wanted to know if there was a built-in function for finding out how many Sundays, Mondays, etc., there are in any given month. Alternatively, I was wondering if anyone had written a function to do this, and if so is it any better than the code I posted.<BR><BR>So, basically go to the first message in this thread (titled "Number of Sundays, Mondays, etc. in a month.") and tell me what you think!<BR><BR>Thanks!

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•