Results 1 to 5 of 5

Thread: no of weekdays between two dates - please help

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Feb 1999
    Location
    Bangalore, Karnataka, India
    Posts
    16

    Post

    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

  2. #2
    Fanatic Member
    Join Date
    Oct 1999
    Location
    MA, USA
    Posts
    523

    Post

    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.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Feb 1999
    Location
    Bangalore, Karnataka, India
    Posts
    16

    Post

    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

  4. #4
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744

    Post

    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
    Serge_Dymkov@vertexinc.com
    Access8484@aol.com
    ICQ#: 51055819



  5. #5

    Thread Starter
    Junior Member
    Join Date
    Feb 1999
    Location
    Bangalore, Karnataka, India
    Posts
    16

    Post

    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

Posting Permissions

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



Click Here to Expand Forum to Full Width