1. sr
Member
Join Date
Dec 1969
Posts
55

the counting days give me error<BR><BR>dim a <BR>a = countDays("08/09/2001","13/09/2001","11/09/2001;12/09/2001")<BR>a1 = countDays("31/08/2001","13/09/2001","11/09/2001;12/09/2001")<BR><BR>I am getting this error<BR>the a should give 2 but I am getting 3<BR>the a1 is give me 0 when I give different month<BR><BR><BR><BR>Function countDays(FromDate, ToDate, HoliDays) <BR>Dim FDate, TDate <BR>Dim Day1, Day2 <BR>Dim Month1, Month2 <BR>Dim Year1, Year2 <BR><BR>Dim tmpDate <BR>Dim i, j, k <BR><BR>countDays = 0 <BR>FDate = Split(FromDate, "/") <BR>TDate = Split(ToDate, "/") <BR><BR>Day1 = FDate(0) <BR>Month1 = FDate(1) <BR><BR>Year1 = FDate(2) <BR><BR>Day2 = TDate(0) <BR>Month2 = TDate(1) <BR>Year2 = TDate(2)<BR><BR>For k = Year1 To Year2 <BR> For j = Month1 To Month2<BR> For i = Day1 To Day2<BR> tmpDate = i&"/"&j&"/"&k<BR> If Weekday(tmpDate, vbMonday) &#060;&#062; 6 And Weekday(tmpDate, vbMonday) &#060;&#062; 7 Then <BR> if HolidayExist(i, j, k, HoliDays) = False Then<BR> countDays = countDays +1<BR> &#039;Response.Write (countDays)<BR> <BR> End If<BR> End If<BR> Next<BR> Next<BR> Next <BR>End Function<BR>Function HolidayExist(Day,Month, Year,HoliDays)<BR> Dim ArrHoliday<BR> Dim ArrDate<BR> Dim i<BR> HolidayExist = False<BR> ArrHoliday = Split(HoliDays, ";")<BR> &#039;for j=0 To UBound(ArrHoliday1)<BR> &#039; ArrHoliday = Split(ArrHoliday1(j), "-")<BR> &#039;Response.Write ("Date<BR>"&ArrHoliday(1))<BR> For i = 0 To UBound(ArrHoliday)<BR> ArrDate = Split(ArrHoliday(i), "/")<BR> &#039;Response.Write (ArrDate(0))<BR> if CInt(ArrDate(0)) = CInt(Day) And CInt(ArrDate(1)) = CInt(Month) And CLng(ArrDate(2)) = CLng(Year) Then <BR> HolidayExist = True<BR> Exit Function<BR> End If <BR> &#039;Next<BR>Next<BR>End Function<BR><BR>

2. Senior Member
Join Date
Dec 1969
Posts
96,118

## That will *NEVER* work!

Try it out BY HAND!<BR><BR>start date: 3/31/2001<BR>end date: 4/1/2001<BR><BR>no holidays<BR><BR>For k = 2001 To 2001 <BR>&nbsp; &nbsp; For j = 3 To 4<BR>&nbsp; &nbsp; &nbsp; &nbsp; For i = 31 To 1<BR><BR>No matter HOW many times the k and j loops would run, the i loop will NEVER EXECUTE.<BR><BR>You need to COMPLETELY THROW AWAY all of that code and start over.<BR><BR>Use DateTime values, instead of taking the strings apart. It makes a *LOT* more sense!<BR><BR>Something as simple as this:<BR><BR>fdate = CDate(FromDate)<BR>tdate = CDate(ToDate)<BR>While tdate &#060; fdate<BR>&nbsp; &nbsp; ... check for holidays and weekends ...<BR>&nbsp; &nbsp; tdate = CDate( tdate + 1 )<BR>Wend<BR>

3. sr
Member
Join Date
Dec 1969
Posts
55

## RE: That will *NEVER* work!

I try this code still I have problem with subract the holidays <BR><BR>holiday= ("Christmas-27/12/2001,BoxinGday-28/12/2001")&#039;Request.QueryString("HD")<BR><BR>&#039 ;to calculat how many week ends<BR>Function HowManyWD(FromDate,ToDate, WD)<BR> HowManyWD = DateDiff("ww", FromDate, ToDate, WD) - Int(WD = WeekDay(FromDate))<BR><BR>End Function<BR>&#039;to find how many holidays fall into the date set<BR>Function HowManyHD(FromDate,ToDate,HD)<BR>Dim i<BR>Dim ArrHoliday<BR>Dim ArrDate<BR> ArrHoliday = Split(HD, ",")<BR> For i = 0 To UBound(ArrHoliday)<BR> ArrDate = Split(ArrHoliday(i), "-")<BR> dim s<BR> s = ArrDate(1)<BR> Dim da<BR> da = DateValue(s)<BR> Response.Write (da)<BR> HowManyHD = DateDiff("d",FromDate,ToDate,da) Next<BR>EnD Function <BR>&#039;to find how many work days<BR>Function HowManyWeekDay(FromDate, ToDate,holiday)<BR> Dim ToDateIsIncluded<BR> ToDateIsIncluded = true<BR> HowManyWeekDay = DateDiff("d", FromDate, ToDate)- ToDateIsIncluded - HowManyWD(FromDate, ToDate, vbSunday) - HowManyWD(FromDate,ToDate, vbSaturday) &#039;-HowManyHD(FromDate,Todate,holiday)<BR><BR>End Function<BR>dim days<BR>days = HowManyWeekDay (aLeaveStart, aLeaveEnd,holiday )<BR>Response.Write (days)

4. Senior Member
Join Date
Dec 1969
Posts
96,118

## Okay, I give up...

...I&#039;ll write the code for you...<BR><BR>&#060;%<BR>&#039; why not start off with an array? <BR>&#039; instead of having to split to get it?<BR>holidays = Array("Christmas-27/12/2001","BoxinGday-28/12/2001")<BR>&#039; create the master array of array of holiday info ONLY ONCE,<BR>&#039; instead of every time you get to the function<BR>For h = 0 To UBound(holidays)<BR>&nbsp; &nbsp; holidays(h) = Split(holidays(h), "-") <BR>&nbsp; &nbsp; holidays(h)(1) = CDate( holidays(h)(1) )<BR>Next<BR><BR>&#039; *NOW* we define the function:<BR>&#039;<BR>Function HowManyWeekDays( byVal fromDate, byVal toDate )<BR>&nbsp; &nbsp; Dim curdate, counter<BR>&nbsp; &nbsp; curDate = CDate(fromDate)<BR>&nbsp; &nbsp; toDate = CDate(fromDate)<BR>&nbsp; &nbsp; counter = 0<BR>&nbsp; &nbsp; While curDate &#060; toDate<BR>&nbsp; &nbsp; &nbsp; &nbsp; If Not IsOffDay(curDate) Then counter = counter + 1<BR>&nbsp; &nbsp; &nbsp; &nbsp; curDate = CDate( curdate + 1 )<BR>&nbsp; &nbsp; Wend<BR>&nbsp; &nbsp; HowManyWeekDays = counter<BR>End Function<BR><BR>Function IsOffDay( dt )<BR>&nbsp; &nbsp; IsOffDay = True<BR>&nbsp; &nbsp; If Weekday(dt) = 1 OR Weekday(dt) = 7 Then Exit Function<BR>&nbsp; &nbsp; For hd = 0 TO UBound( holidays )<BR>&nbsp; &nbsp; &nbsp; &nbsp; If dt = holidays(hd)(1) Then Exit Function<BR>&nbsp; &nbsp; Next<BR>&nbsp; &nbsp; IsOffDay = False<BR>End Function<BR>%&#062;<BR><BR>I made the list of holidays a fixed array, instead of passing it to the function. SURELY you wouldn&#039;t use a different list of holidays for every request, would you? Don&#039;t all employees have the same holidays?<BR><BR>

5. Senior Member
Join Date
Dec 1969
Posts
96,118

## Oops...a typo...

I decided to test the code. Found one error:<BR><BR>In the code:<BR>&nbsp; &nbsp; curDate = CDate(fromDate) <BR>&nbsp; &nbsp; toDate = CDate(fromDate) <BR>&nbsp; &nbsp; counter = 0 <BR><BR>The middle line is wrong. It should be:<BR>&nbsp; &nbsp; toDate = CDate(toDate) <BR><BR>Other than that, the code seemed to work. Sorry. Comes from typing off the top of my head instead of using an editor and testing.<BR><BR><BR><BR>

6. Senior Member
Join Date
Dec 1969
Posts
1,843

## You're a better man then I <EOM>

nt

7. Senior Member
Join Date
Dec 1969
Posts
96,118

## Or more of a sucker?

Since I let him con me into writing the code?<BR><BR>Sigh. But he was *SO* lost. Did you see that code? Three different ways to do everything and none of them matching and...<BR><BR>I do have to wonder how people get into ASP with absolutely zero preparation. Well, not fair... I know a coupld of the stories: A friend is a graphic designer who was asked to learn ASP because his company couldn&#039;t afford a programmer. But you know, I think that they ended up spending twice what they would have if they&#039;d hired a contractor...because it took him 4 times as long (at least) as the contractor would have taken. And even if he made half the salary...<BR><BR>But surely not *all* the tyros at ASP are in that same situation? So where is all the professional (or professorial, perhaps, or managerial) help they should be getting???<BR><BR>

#### Posting Permissions

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