VB.NET: How to find the number of weekend days (or bussiness days) between 2 dates

Posted on 2005-05-06
Last Modified: 2011-09-20
Given 2 date time picker controlls, one for a startDate and the other one for an endDate, how would I go about finding either the number of weekend days or bussiness days between 2 given dates (it doesn't matter which since I can just substract the those days from the total number of days between the two dates) Your help on this issue will be greatly appreciated.
Question by:isrxl
    LVL 28

    Accepted Solution

    Function BusinessDateDiff(ByVal startDate As Date, ByVal endDate As Date,
     Optional ByVal saturdayIsHoliday As Boolean = True) As Integer
        Dim incr As Integer

        ' incr can be +1 or -1
        If startDate < endDate Then incr = 1 Else incr = -1

        Do Until startDate = endDate
            ' skip to previous or next day
            startDate = startDate.AddDays(incr)
            If startDate.DayOfWeek <> DayOfWeek.Sunday AndAlso _
                (startDate.DayOfWeek <> DayOfWeek.Saturday Or Not
    saturdayIsHoliday) Then
                ' if it's a weekday add/subtract one to the result
                BusinessDateDiff += incr
            End If
        ' when the loop is exited the function name contains the correct
    End Function

    Author Comment

    Thank you very much iboutchkine, that's short and sweet.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Article by: Kraeven
    Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
    Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now