soon keong soon keong - 28 days ago 7
C# Question

Calculate End Date when specific duration of days given

I am developing c#.net solution where i have to calculate End Date from start date in date time picker based on provided duration date.(excluding weekends)

i.e.


Start Date: 02/09/2015 days: 5

Start Date = 02/09/2015 + 5 days (Excludes weekends)

End Date = 08/09/2015


thanks you,

Answer

Try this,

var startDate = DateTime.Now;  // Put your actual start date here
for (int i = 0; i < 5;)        // Put number of days to add instead of 5
{
    startDate = startDate.AddDays(1);
    if (startDate.DayOfWeek == DayOfWeek.Saturday || startDate.DayOfWeek == DayOfWeek.Sunday)
    {              
        continue;
    }
    i++;
}
var finalDate = startDate;

enter image description here