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

Day in Year/Mont

Status
Not open for further replies.

SBerthold

Programmer
Sep 20, 2002
1,014
DE
What would be the best way to determine:

1-The number of Mondays in a given year?

2-The number of Mondays in a given month?

3-The number of weekends in a given year

4. The number of weekends in a given Month

I am looking to use a different function for each of these, passing the day (such as Monday or Tuesday), the year, and the month.

The function I can write myself. Just need a good logic for doing this and the proper use of VB functions.
 
The first two can easily be handled be calling a function which returns an integer. The function prototype would be something like the following:

Function DaysInPeriod (WhichDay as Integer, WhichPeriod as Integer) as Integer

WhichDay would refer to the day of the week with values 1 thru 7. WhichPeriod would refer to either the month or the year, depending on the value. Values 1 thru 12 would refer to the months, and any four digit value would represent a year.

With respect to the weekend, you must first define what is a weekend. Is just a Saturday, or just a Sunday considered a weekend, or do you need both days in the same period to count?

Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 

Thank you for responding.

Actually, what I meant was that I can create the function myself, but the logic in getting the number of, say, Mondays in a year with-out having to loop through each day, is missing.

I am looking for another logic in getting/calculating these results.

Any idea?
 
For how many Mondays in a given year, there are always at least 52 (365/7 = 52.142857 and 366/7 = 52.285714), so you don't need to concern yourself with all of the ones in the middle. The only time that there are 53 Mondays in any given year is when there is a Monday in the first two days of the year AND in the last two days of the year. Why test two days instead of one on each end? For leap years. 365 days means that the first and last day of the year fall on the same day of the week. 366 days mean that the first day of the year and the second to last day of the year fall on the same day. So, another stab at the logic could quickly eliminate from the 53 Mondays category any year beginning on a Tuesday through Saturday. Years which begin on a Monday can only have 53 Mondays if they are a leap year. Years which begin on Monday will always have 53 Mondays.

The logic for Mondays in a month is very similar. Since all months have at least 28 days (exactly 4 weeks) they all have at least 4 Mondays. Since they also have a maximum of 31 days (31/7 = 4.428571 weeks) no months can have more than 5 Mondays. Months can only have 5 Mondays if there is a Monday in both the first 3 days AND the last three days of the month. From another perspective, if a month begins on a Tuesday through Friday, it can only have 4 Mondays. If it begins on a Saturday, it can have 5 Mondays only if the month has 31 days. If it begins on a Sunday, it can have 5 Mondays only if the month has 30 or 31 days. If it begins on a Monday, it has 5 Mondays in it unless the month only has 28 days.

Weekends are similar, but you have to define what constitutes a weekend first. BlackburnKL
 


I'm still digesting this to determine a simple method.

Right now it seems that I need to loop through the last few days of the year(or month) only, which do not divide by 7.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top