Hi every one,
I m new to program side, I want to create one simple date operation in asp.net
the procedure is simple,
Step1: User want to select course start day in calender control
Step2:Then want to enter duration of the course(in days, like 20 days) in textbox.
Step3:After selection user wants to click the button to calculate the course end date excluding weekend days and holiday list.
step4:Result should show the enddate of course in label or textbox.
Can any one provide the logic for that, i m new for asp.net.
Thank you
Praveen
Loading
Zoran HorvatPosted Jul 13, 2011, 5:51 AM
Zoran
praveen bahubaliPosted Jul 14, 2011, 3:08 PM
Zoran HorvatPosted Jul 13, 2011, 5:36 AM
Simplest method, though not the most efficient one, is to iterate through days and to count out Saturdays and Sundays along the way:
DateTime startDate = DateTime.Now;
int workingDaysToAdd = 7;
DateTime endDate = startDate;
while (workingDaysToAdd > 0)
{
if (endDate.DayOfWeek != DayOfWeek.Saturday && endDate.DayOfWeek != DayOfWeek.Sunday)
workingDaysToAdd--;
if (workingDaysToAdd > 0)
endDate = endDate.AddDays(1);
}
Console.WriteLine("Starting date: {0:MM/dd/yyyy}; Ending date: {1:MM/dd/yyyy}", startDate, endDate);
This code prints output:
Starting date: 07/13/2011; Ending date: 07/21/2011
In this case both July 13 and July 21 are included in the count, which I believe you wanted to be done. This procedure works fine even if starting day falls on weekend.
Zoran