Here is a little piece of code using which you can calculate 5th, 10th or nth (here n can be any number) working (business) day from today (or any given date).
Note that we are assumiung that Saturday and Sunday are weekend, you can change the code if your weekend is not on Saturday, Sunday.
btnCalc is the button on which's click we are calling the function to calculate and lblDate is the label where we are showing the nth working (business) date.
Note that we are assumiung that Saturday and Sunday are weekend, you can change the code if your weekend is not on Saturday, Sunday.
btnCalc is the button on which's click we are calling the function to calculate and lblDate is the label where we are showing the nth working (business) date.
C# (CSharp)
protected void btnCalc_Click(object
sender, EventArgs e)
{
//Here I am
calculating 10th working (business) day from today. You can replace the
parameter 10 with your required number of working (business) day
//Also I am
calculating from today, you can give your required Date as parameter as well at
the place of DateTime.Now
lblDate.Text =
calculateNthWorkingDay(10, DateTime.Now).ToString();
}
public DateTime calculateNthWorkingDay(int n, DateTime
pFromDate) //n=Number of working day.
{
int
mWorkingDays = 0;
for (int i = 1; i <= n; i++)
{
pFromDate = pFromDate.AddDays(1);
if
(!IsWeekEnd(pFromDate))
mWorkingDays++;
else
n++;
}
return
pFromDate;
}
public bool IsWeekEnd(DateTime
pDateTime)
{
bool
isWeekEnd = false;
if
((pDateTime.DayOfWeek == DayOfWeek.Saturday)
|| (pDateTime.DayOfWeek == DayOfWeek.Sunday))
{
isWeekEnd = true;
}
return
isWeekEnd;
}
VB.Net
Protected Sub btnCalc_Click(sender As
Object, e As EventArgs)
'Here I am
calculating 10th working (business) day from today. You can replace the
parameter 10 with your required number of working (business) day
'Also I am
calculating from today, you can give your required Date as parameter as well at
the place of DateTime.Now
lblDate.Text =
calculateNthWorkingDay(10, DateTime.Now).ToString()
End Sub
Public Function calculateNthWorkingDay(n As Integer, pFromDate
As DateTime)
As DateTime
'n=Number of
working day.
Dim
mWorkingDays As Integer
= 0
For i As Integer = 1 To n
pFromDate = pFromDate.AddDays(1)
If Not IsWeekEnd(pFromDate) Then
mWorkingDays += 1
Else
n += 1
End
If
Next
Return
pFromDate
End Function
Public Function IsWeekEnd(pDateTime As
DateTime) As
Boolean
Dim
isWeekEnd As Boolean
= False
If
(pDateTime.DayOfWeek = DayOfWeek.Saturday) OrElse (pDateTime.DayOfWeek = DayOfWeek.Sunday) Then
isWeekEnd1 = True
End If
Return
isWeekEnd1
End Function
No comments:
Post a Comment