Sososo
Sososo

Reputation: 19

Exclude Holidays Between two days

Hi everybody i'm working a project about calculate specific working days. My conditions are : Saturday is not holiday, it is working day.

I wrote this code everyting is okey skipped Sundays only , but i want to skip if contains a holiday date.

Problem : skipping Sunday but not skipping passing holiday list values.

Future Date Calculate function :

public DateTime CalculateFutureDate(DateTime fromDate, int numberofWorkDays,
                                     List<DateTime> holidays)
{
    var futureDate = fromDate;
    for (var i = 0; i < numberofWorkDays; i++)
    {
        if (
            futureDate.DayOfWeek == DayOfWeek.Sunday
           || (holidays != null && holidays.Contains(futureDate)))
        {
            futureDate = futureDate.AddDays(1);
            numberofWorkDays++;
        }
        else
        {
            futureDate = futureDate.AddDays(1);
        }
    }
    while (
           futureDate.DayOfWeek == DayOfWeek.Sunday
            || (holidays != null && holidays.Contains(futureDate)))
    {
        futureDate = futureDate.AddDays(1);
    }

    return futureDate;
}

Main function:

List<DateTime> holidayslist = new List<DateTime>();
holidayslist.Add(new DateTime(2016, 09, 7));
holidayslist.Add(new DateTime(2016, 09, 8));
holidayslist.Add(new DateTime(2016, 09, 9));
DateTime izinbaslangic = Convert.ToDateTime(dtpİzinBaslangicTarihi.Value, System.Globalization.CultureInfo.CreateSpecificCulture("tr-TR").DateTimeFormat);
dtpİzinBitisTarihi.Value = CalculateFutureDate(izinbaslangic, Int32.Parse(tbİzinGunu.Text), holidayslist);

Inputs : izinbaslangic -> datetimepicker value and workingdays -> tbizingunu value

Expecting Outputs : exclude holiday and weekends show in another datetimepicker new date.

Output : Only skipping Weekends.Not skipping holidays.

Expecting Output Image : enter image description here

Upvotes: 0

Views: 1253

Answers (1)

Steve
Steve

Reputation: 216302

I would rewrite your code in the following way

public DateTime CalculateFutureDate(DateTime fromDate, int numberofWorkDays,
                                     List<DateTime> holidays)
{
    var futureDate = fromDate;
    while (numberofWorkDays != 0)
    {
        if (!isHoliday(futureDate, holidays))
            numberofWorkDays--;
        futureDate = futureDate.AddDays(1);
    }
    while (isHoliday(futureDate, holidays))
        futureDate = futureDate.AddDays(1);
    return futureDate;
}

bool isHoliday(DateTime testDate, List<DateTime>holidays)
{
    return (testDate.DayOfWeek == DayOfWeek.Sunday
           || (holidays != null && holidays.Contains(testDate.Date)));
}

The idea is simply to create a loop until the number of working days required is reduced to zero. Also isolating the logic to test for an holiday will help a lot in a better understanding of the code and avoid a dangerous duplication of the same logic

List<DateTime> holidayslist = new List<DateTime>();
holidayslist.Add(new DateTime(2016, 09, 7));
holidayslist.Add(new DateTime(2016, 09, 8));
holidayslist.Add(new DateTime(2016, 09, 9));

DateTime start = new DateTime(2016,9,7);
DateTime ending = CalculateFutureDate(start, 1, holidayslist);
Console.WriteLine(ending.ToString()); // 12/09/2016 00:00:00

Upvotes: 3

Related Questions