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