Calculate Number of days between two dates excluding holidays using C# and VB.Net in ASP.Net

Last Reply one year ago By pandeyism

Posted one year ago

Mr.Muddasar I have an issue calculate no of days between two dates Now I have used the below code working fine which is exclude Saturday,Sunday But real issues, special Holiday like Christmas/New Year and country National day or any other Govt holiday. Please advice me Maideen

 

    Public Shared Function GetBusinessDays(ByVal startD As DateTime, ByVal endD As DateTime) As Double
        Dim calcBusinessDays As Double = 1 + ((endD - startD).TotalDays * 5 - (startD.DayOfWeek - endD.DayOfWeek) * 2) / 7
        If endD.DayOfWeek = DayOfWeek.Saturday Then calcBusinessDays -= 1
        If startD.DayOfWeek = DayOfWeek.Sunday Then calcBusinessDays -= 1
        Return calcBusinessDays
    End Function

 

You are viewing reply posted by: pandeyism one year ago.
Posted one year ago

Hey maideen,

Create a list DateTime for holiday. Then check the DateTime with condition.

Please refer below sample.

HTML

StartDate :
<asp:TextBox runat="server" ID="txtStartDate" />
<br />
EndDate :<asp:TextBox runat="server" ID="txtEndDate" />
<br />
<asp:Button Text="Calculate" runat="server" OnClick="Calculate" />

Code

C#

protected void Calculate(object sender, EventArgs e)
{
    List<DateTime> holidays = new List<DateTime>();
    // Manually adding all holiday list.
    holidays.Add(new DateTime(DateTime.Now.Year, 1, 1)); // New Year.
    holidays.Add(new DateTime(DateTime.Now.Year, 1, 14)); // Makar Sankranti.
    holidays.Add(new DateTime(DateTime.Now.Year, 1, 26)); // National Holiday.

    DateTime startDate = Convert.ToDateTime(txtStartDate.Text);
    DateTime endDate = Convert.ToDateTime(txtEndDate.Text);
    int days = 0;

    for (DateTime date = startDate; date <= endDate; date = date.AddDays(1))
    {
        if (startDate.DayOfWeek != DayOfWeek.Saturday && startDate.DayOfWeek != DayOfWeek.Sunday && !holidays.Contains(date))
        {
            days++;
        }
        startDate = startDate.AddDays(1);
    }
    Response.Write("Number of days between " + Convert.ToDateTime(txtStartDate.Text).ToShortDateString() + " and "
        + Convert.ToDateTime(txtEndDate.Text).ToShortDateString() + " excluding special holiday is " + days.ToString());
}

VB.Net

Protected Sub Calculate(ByVal sender As Object, ByVal e As EventArgs)
    Dim holidays As List(Of DateTime) = New List(Of DateTime)()
    holidays.Add(New DateTime(DateTime.Now.Year, 1, 1))
    holidays.Add(New DateTime(DateTime.Now.Year, 1, 14))
    holidays.Add(New DateTime(DateTime.Now.Year, 1, 26))
    Dim startDate As DateTime = Convert.ToDateTime(txtStartDate.Text)
    Dim endDate As DateTime = Convert.ToDateTime(txtEndDate.Text)
    Dim days As Integer = 0
    Dim date1 As DateTime = startDate
    While date1 <= endDate
        If startDate.DayOfWeek <> DayOfWeek.Saturday AndAlso startDate.DayOfWeek <> DayOfWeek.Sunday AndAlso Not holidays.Contains(date1) Then
            days += 1
        End If
        startDate = startDate.AddDays(1)
        date1 = date1.AddDays(1)
    End While

    Response.Write("Number of days between " & Convert.ToDateTime(txtStartDate.Text).ToShortDateString() & " and " _
                   + Convert.ToDateTime(txtEndDate.Text).ToShortDateString() & " excluding special holiday is " + days.ToString())
End Sub

Screenshot