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
Printable View
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
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.
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
Try this:
Code: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
[email protected]
[email protected]
ICQ#: 51055819
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