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

kmuthu1212
 
on Sep 02, 2021 10:54 PM
Sample_138229.zip
769 Views

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

In my case, I would like to Saturday set as 0.5.

Kindly help me to give me the solution asap

Thank you

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Sep 02, 2021 11:14 PM
on Sep 03, 2021 05:35 AM

Hi kmuthu1212,

Create a list DateTime for special 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="OnCalculate" />
<hr /><asp:Label ID="lblMessage" runat="server" />

Code

C#

protected void OnCalculate(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, 26)); // National Holiday.
    holidays.Add(new DateTime(2021, 10, 18)); // Eid-E-Milad.

    DateTime startDate = Convert.ToDateTime(txtStartDate.Text);
    DateTime endDate = Convert.ToDateTime(txtEndDate.Text);
    double days = 0;
    for (DateTime dateValue = startDate; dateValue <= endDate; dateValue = dateValue.AddDays(1))
    {
        if (startDate.DayOfWeek != DayOfWeek.Sunday && !holidays.Contains(dateValue))
        {
            if (startDate.DayOfWeek == DayOfWeek.Saturday)
            {
                days = days + 0.5;
            }
            else
            {
                days = days + 1;
            }
        }

        startDate = startDate.AddDays(1);
    }
    lblMessage.Text = "Number of working days between " + Convert.ToDateTime(txtStartDate.Text).ToShortDateString() + " and "
                    + Convert.ToDateTime(txtEndDate.Text).ToShortDateString() + " excluding special holiday is " + days.ToString();
}

VB.Net

Protected Sub OnCalculate(ByVal sender As Object, ByVal e As EventArgs)
    Dim holidays As List(Of DateTime) = New List(Of 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, 26)) 'National Holiday.
    holidays.Add(New DateTime(2021, 10, 18)) 'Eid-E-Milad.
    Dim startDate As DateTime = Convert.ToDateTime(txtStartDate.Text)
    Dim endDate As DateTime = Convert.ToDateTime(txtEndDate.Text)
    Dim days As Double = 0
    Dim dateValue As DateTime = startDate
    While dateValue <= endDate
        If startDate.DayOfWeek <> DayOfWeek.Sunday AndAlso Not holidays.Contains(dateValue) Then
            If startDate.DayOfWeek = DayOfWeek.Saturday Then
                days = days + 0.5
            Else
                days = days + 1
            End If
        End If
        startDate = startDate.AddDays(1)
        dateValue = dateValue.AddDays(1)
    End While
    lblMessage.Text = "Number of working days between " & Convert.ToDateTime(txtStartDate.Text).ToShortDateString() & " and " _
                    & Convert.ToDateTime(txtEndDate.Text).ToShortDateString() & " excluding special holiday is " & days.ToString()
End Sub

Screenshot