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

weekends in month 2

Status
Not open for further replies.

thompom

Technical User
Dec 4, 2006
395
GB
hi,
the function below works out the days in a month, but how would i get it to not count weekends?

Code:
function getDaysInMonth(strMonth,strYear)
dim strDays	 
    Select Case cint(strMonth)
        Case 1,3,5,7,8,10,12:
			strDays = 31
			
        Case 4,6,9,11:
		strDays = 30
        Case 2:
		if  ((cint(strYear) mod 4 = 0  and  _
                 cint(strYear) mod 100 <> 0)  _
                 or ( cint(strYear) mod 400 = 0) ) then
		  strDays = 29
		else
		  strDays = 28 
		end if	
    End Select 
    
    getDaysInMonth = strDays

end function
 
This would be a fun one to try to do as a one liner. Rather than just give you the answer on it, I'll lead you through how I would work out an answer, as there are multiple ways to skin this cat and if I just give you an answer without the method i used to get it, you might have more difficulty integrating it into your code.

First I usually figure out what my rules are. I am going to base this logic off of the fact that the first 28 days of a month are guaranteed to have 8 weekend days and go from there.
Code:
If a month has 28 days, then it must have 8 weekend days in it

The 28th is one week day earlier then then 1st of the month.

If a month has 29 days and the 29th day is Saturday or Sunday then it has 9 weekend days
If a month has 29 days and the 29th day is not Saturday or Sunday then it has 8 weekend days

If a month has 30 days and the 29th day is Saturday then it has 10 weekend days
If a month has 30 days and the 29th day is Sunday or Friday then it has 9 weekend days
If a month has 30 days and the 29th day is not Friday or Saturday or Sunday, then it still has 8 days

If a month has 31 days and the 29th day is Friday or Saturday, then it has 10 weekend days
If a month has 31 days and the 29th day is Thursday or Sunday, then it has 9 weekend days
If a month has 31 days and the 29th day is not Thursday through Sunday, then it has 8 weekend days

Now if we quickly turn this into a case statement or series of actual if statements we are basically done. It could be shorter and there are several ways to condense the logic, but we have a starting place to do so:
Code:
Dim stdate, enDate, numWeekEndDays
stDate = DateSerial(strYear, strmonth, "1")
enDate = DateAdd("d",-1,DateAdd("m",1,stDate))

Select Case Day(enDate)
   Case 28
      numWeekEndDays = 8
   Case 29
      If WeekDay(stDate) = 1 Or WeekDay(stDate) = 7 Then
         numWeekEndDays = 9
      Else
         numWeekEndDays = 8
      End If
   Case 30
      If WeekDay(stDate) = 1 Then
         numWeekEndDays = 10
      ElseIf WeekDay(stDate) = 6 or WeekDay(stDate) = 1 Then
         numWeekEndDays = 9
      Else
         numWeekEndDays = 8
      End If
   Case 31
      If WeekDay(stDate) = 6 or WeekDay(stDate) = 7 Then
         numWeekEndDays = 10
      ElseIf WeekDay(stDate) = 5 or WeekDay(stDate) = 1 Then
         numWeekEndDays = 9
      Else
         numWeekEndDays = 8
      End If
End Select

Response.Write "Number of week days is " & (Day(enDate) - numWeekEndDays)

I'll warn you that I may have made some minor error converting the logic to code, as it's untested and I'm still on my first cup of coffee.

If you do decide to compact the logic down further, it would be nice to see where you go with it. Sometimes trying to make the most elegant function possible for small problems like this is a lot of fun.

-T

Best MS KB Ever:
 
Nice work Tarwn!
star.gif
 
much thanks Tarwn - i bet your a fantastic chess player!

not sure if its quite there - april and september are out
but have got enough to get going, thanks again

Code:
function getDaysInMonth(strMonth,strYear)
Dim stdate, enDate, numWeekEndDays
dim strDays	 
stDate = DateSerial(strYear, strmonth, "1")

Select Case cint(strMonth)
Case 1,3,5,7,8,10,12:
strDays = 31
If WeekDay(stDate) = 6 or WeekDay(stDate) = 7 Then
numWeekEndDays = 10
ElseIf WeekDay(stDate) = 5 or WeekDay(stDate) = 1 Then
numWeekEndDays = 9
Else
numWeekEndDays = 8
End If

Case 4,6,9,11:
strDays = 30
If WeekDay(stDate) = 1 Then
numWeekEndDays = 10
ElseIf WeekDay(stDate) = 6 or WeekDay(stDate) = 1 Then
numWeekEndDays = 9
Else
numWeekEndDays = 8
End If
	  
Case 2:
if  ((cint(strYear) mod 4 = 0  and  _
cint(strYear) mod 100 <> 0)  _
or ( cint(strYear) mod 400 = 0) ) then
strDays = 29
		  
If WeekDay(stDate) = 1 Or WeekDay(stDate) = 7 Then
numWeekEndDays = 9
Else
numWeekEndDays = 8
End If	  
else
strDays = 28 
numWeekEndDays = 8
end if	

End Select 

strdays = strdays - numWeekEndDays
getDaysInMonth = strDays

end function
 
hi - just thought id post this part that needed changing
for april, june etc

Code:
Case 4,6,9,11:
strDays = 30
If WeekDay(stDate) = 1 or WeekDay(stDate) = 6 Then
numWeekEndDays = 9
ElseIf WeekDay(stDate) = 7 Then
numWeekEndDays = 10
Else
numWeekEndDays = 8
End If
 
Sorry about that, it's always a little harder working with weekdays in VBScript due to the 1-based weekday system it uses, so sometimes I'm thinking one day and writing the number for another :p Glad it worked out for you.

-T

Best MS KB Ever:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top