PDA

Click to See Complete Forum and Search --> : no of weekdays between two dates - please help


devarajulu
Nov 4th, 1999, 03:48 AM
hi all,
i have a requirement in my project to find the number of weekdays in between any two given dates. can anyone help me with the code for that. it has to go into production within two days. i would greatly appreciate any help
thanks
Dev

QWERTY
Nov 4th, 1999, 04:02 AM
What do you mean by the number of weekdays. If you mean just the number of Days You could use DateDiff Function.



------------------
Visual Basic Programmer
-----------------
PolComSoft
You will hear a lot about it.

devarajulu
Nov 4th, 1999, 04:08 AM
DateDiff gives me the total number of days in between the two dates. i want the number of days excluding saturdays and sundays. i should have phrased it as business days or working days probably instead of weekdays

Serge
Nov 4th, 1999, 05:17 AM
Try this:


Public Function GetBusinessDaysDiff(pStartDate As Date, pEndDate As Date) As Long
Dim lngDays As Long
Dim lngWeeks As Long
Dim intDifference As Integer

If pStartDate > pEndDate Then
NumberOfBusinessDays = 0
Else
lngDays = DateDiff("d", pStartDate, pEndDate)
lngWeeks = DateDiff("ww", pStartDate, pEndDate)
intDifference = 0
If Weekday(pStartDate) = vbSunday And _
Weekday(pEndDate) <> vbSunday And _
Weekday(pEndDate) <> vbSaturday Then
intDifference = -1
ElseIf Weekday(pStartDate) = vbSunday And _
Weekday(pEndDate) = vbSunday Then
intDifference = 0
ElseIf Weekday(pEndDate) = vbSunday Then
intDifference = 1
End If
If Weekday(pEndDate) <> vbSunday And Weekday(pEndDate) <> vbSaturday Then
intDifference = intDifference + 1
End If
GetBusinessDaysDiff = (lngDays - (lngWeeks * 2)) + intDifference
End If
End Function



Usage: GetBusinessDaysDiff StartDate, EndDate
Example: MsgBox GetBusinessDaysDiff Date, Date + 5


Regards,

------------------

Serge

Software Developer
Serge_Dymkov@vertexinc.com
Access8484@aol.com
ICQ#: 51055819

devarajulu
Nov 4th, 1999, 07:16 AM
Thanks a lot for your code serge. i had to subtract one day from the final result to exclude the start date. thanks anyway
Dev