Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Hi guys;

I want to generate end date excluding weekends.

I have tried this but didn't give me expected result:

VB
<pre>    <WebMethod()> _
    Public Shared Function GenerateEndDate(startDate As String, numberOfDays As Integer) As String
        If IsDate(startDate) Then
            ' generate end date
            Dim endDate As Date = CDate(startDate).AddDays(GetNoOfWorkingDays(CDate(startDate), numberOfDays))  'DateAdd(DateInterval.Day, numberOfDays - 1, CDate(startDate))
            ' get number of working days

            Return endDate.ToShortDateString
        End If
        Return "N/A"
    End Function

    Private Shared Function GetNoOfWorkingDays(ByVal startDate As Date, ByVal noOfDays As Int32) As Int32
        Dim counter As Integer = 0
        Dim endDate As DateTime = DateAdd(DateInterval.Day, noOfDays, startDate)
        While startDate < endDate
            startDate = startDate.AddDays(1)
            Dim dayNoInWeek = CInt(startDate.DayOfWeek)
            If dayNoInWeek <> 0 Then
                If dayNoInWeek <> 6 Then
                    counter += 1
                End If
            End If
        End While

        Return noOfDays + counter
    End Function
Posted

1 solution

This assumes only dates are compared, not times, and work week is Monday-Friday

C#
private int NumberOfWorkDays(DateTime start, int numberOfDays)
{
    int workDays = 0;

    DateTime end = start.AddDays(numberOfDays);

    while(start != end)
    {
        if(start.DayOfWeek != DayOfWeek.Saturday && start.DayOfWeek != DayOfWeek.Sunday)
        {
            workDays++;
        }

        start = start.AddDays(1);
    }

    return workDays;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900