p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

proasp_codeclinic thread: Caluculate number of days


Message #1 by Sumi <rsumathy@e...> on Wed, 12 Sep 2001 04:29:18 -0700 (PDT)
Thank you for your time to write the code but I wrode differently and it is
working fine again Thank you
sr


On Thu, 13 Sep 2001 06:01:03 -0700 (PDT), Code Clinic wrote:

|  I have changed the 2 functions according to the
|  requirement. As the total working days could be
|  calculated, I have changed my code to identify the
|  holidays and minus it from Total Working Days.
|  
|  countDays function has 4 parameters - From date, To
|  date, Holiday string, Total Working days.
|  
|  HolidayExist have 3 parameters - From date, To date,
|  Holiday date
|  
|  a = countDays("30/07/2001", "13/08/2003", "Good
|  Friday-30/7/2001, Labour Day-9/3/2002", 100)
|  
|  The above statement will return 98 as total working
|  days.
|  
|  
|  Function countDays(FromDate, ToDate, HoliDays,
|  TotalDays)
|  Dim HolidayString
|  Dim SplitDate
|  Dim DateString
|  Dim CheckDate
|  
|      
|      DateString = ""
|      HolidayString = Split(HoliDays, ",")
|      
|      For i = 0 To UBound(HolidayString)
|          SplitDate = Split(HolidayString(i), "-")
|          If i = 0 Then
|              DateString = DateString &
|  Trim(SplitDate(1))
|          Else
|              DateString = DateString & ";" &
|  Trim(SplitDate(1))
|          End If
|      Next
|      
|      CheckDate = Split(DateString, ";")
|      For i = 0 To UBound(CheckDate)
|          If HolidayExist(FromDate, ToDate,
|  CheckDate(i)) = True Then
|              TotalDays = TotalDays - 1
|          End If
|      Next
|      countDays = TotalDays
|  End Function
|  
|  
|  
|  Private Function HolidayExist(FrDate, ToDate, HoDate)
|  Dim arrFrDate
|  Dim arrToDate
|  Dim arrHoDate
|  
|  Dim FrDay
|  Dim FrMonth
|  Dim FrYear
|  
|  Dim ToDay
|  Dim ToMonth
|  Dim ToYear
|  
|  Dim HoDay
|  Dim HoMonth
|  Dim HoYear
|  
|  Dim cntDay
|          
|      HolidayExist = False
|      
|      arrFrDate = Split(FrDate, "/")
|      arrToDate = Split(ToDate, "/")
|      arrHoDate = Split(HoDate, "/")
|      
|      FrDay = CInt(arrFrDate(0))
|      FrMonth = CInt(arrFrDate(1))
|      FrYear = CInt(arrFrDate(2))
|      
|      ToDay = CInt(arrToDate(0))
|      ToMonth = CInt(arrToDate(1))
|      ToYear = CInt(arrToDate(2))
|      
|      HoDay = CInt(arrHoDate(0))
|      HoMonth = CInt(arrHoDate(1))
|      HoYear = CInt(arrHoDate(2))
|      
|      'This loop checks holiday year, month and day is
|  within the range
|      
|      If HoYear >= FrYear And HoYear <= ToYear Then
|          If HoYear = FrYear And HoYear = ToYear Then
|              If FrMonth < ToMonth Then
|                  If HoMonth >= FrMonth And HoMonth <
|  ToMonth Then
|                      If HoMonth = FrMonth Then
|                          If HoMonth = 1 Or HoMonth = 3
|  Or HoMonth = 5 Or HoMonth = 7 Or HoMonth = 8 Or
|  HoMonth = 10 Or HoMonth = 12 Then
|                              cntDay = 31
|                          ElseIf HoMonth = 4 Or HoMonth
|  = 6 Or HoMonth = 9 Or HoMonth = 11 Then
|                              cntDay = 30
|                          ElseIf HoMonth = 2 Then
|                              cntDay = IIf(((k Mod 4 
|  0) And ((Not (k Mod 100 = 0)) Or (k Mod 400 = 0))),
|  29, 28)
|                          End If
|                          
|                          If HoDay >= FrDay And HoDay <
|  cntDay Then
|                              HolidayExist = True
|                              Exit Function
|                          Else
|                              Exit Function
|                          End If
|                      ElseIf HoMonth = ToMonth Then
|                          If HoDay >= 1 And HoDay <
|  ToDay Then
|                              HolidayExist = True
|                              Exit Function
|                          Else
|                              Exit Function
|                          End If
|                      Else
|                          HolidayExist = True
|                          Exit Function
|                      End If
|                  Else
|                      Exit Function
|                  End If
|              ElseIf FrMonth = ToMonth Then
|                  If HoMonth = FrMonth Then
|                      If HoDay >= FrDay And HoDay <
|  ToDay Then
|                          HolidayExist = True
|                          Exit Function
|                      Else
|                          Exit Function
|                      End If
|                  Else
|                      Exit Function
|                  End If
|              End If
|          ElseIf (HoYear = FrYear) Then
|              If (HoMonth >= FrMonth And HoMonth <= 12)
|  Then
|                  If HoMonth = FrMonth Then
|                      If HoMonth = 1 Or HoMonth = 3 Or
|  HoMonth = 5 Or HoMonth = 7 Or HoMonth = 8 Or HoMonth 
|  10 Or HoMonth = 12 Then
|                          cntDay = 31
|                      ElseIf HoMonth = 4 Or HoMonth = 6
|  Or HoMonth = 9 Or HoMonth = 11 Then
|                          cntDay = 30
|                      ElseIf HoMonth = 2 Then
|                          cntDay = IIf(((k Mod 4 = 0)
|  And ((Not (k Mod 100 = 0)) Or (k Mod 400 = 0))), 29,
|  28)
|                      End If
|                      
|                      If HoDay >= FrDay And HoDay <
|  cntDay Then
|                          HolidayExist = True
|                          Exit Function
|                      Else
|                          Exit Function
|                      End If
|                  Else
|                      HolidayExist = True
|                      Exit Function
|                  End If
|              Else
|                  Exit Function
|              End If
|          ElseIf (HoYear = ToYear) Then
|              If (HoMonth >= 1 And HoMonth <= ToMonth)
|  Then
|                  If HoMonth = ToMonth Then
|                      If HoDay >= 1 And HoDay <= ToDay
|  Then
|                          HolidayExist = True
|                          Exit Function
|                      Else
|                          Exit Function
|                      End If
|                  Else
|                      HolidayExist = True
|                      Exit Function
|                  End If
|              Else
|                  Exit Function
|              End If
|          Else
|              HolidayExist = True
|              Exit Function
|          End If
|      Else
|          Exit Function
|      End If
|      
|  End Function
|  
|  Note: This code assumes that From date is less than To
|  date and all Dates are valid. 
|  
|  Previous code may have error as I have no time
|  to test. Hope so this code will help you.
|  
|  
|  
$subst('Email.Unsub')
|  


Sumi R
rsumathy@e...

  Return to Index