How to get 14 days prior to the given date avoiding holidays
c#, sql-server-2008
Solution
I would calculate the Date using a function like the one below (which i use)
public static DateTime AddBusinessDays(DateTime date, int days)
{
if (days == 0) return date;
if (date.DayOfWeek == DayOfWeek.Saturday)
{
date = date.AddDays(2);
days -= 1;
}
else if (date.DayOfWeek == DayOfWeek.Sunday)
{
date = date.AddDays(1);
days -= 1;
}
date = date.AddDays(days / 5 * 7);
int extraDays = days % 5;
if ((int)date.DayOfWeek + extraDays > 5)
{
extraDays += 2;
}
int extraDaysForHolidays =-1;
//Load holidays from DB into list
List<DateTime> dates = GetHolidays();
while(extraDaysForHolidays !=0)
{
var days = dates.Where(x => x >= date && x <= date.AddDays(extraDays)).Count;
extraDaysForHolidays =days;
extraDays+=days;
}
return date.AddDays(extraDays);
}
Haven't tested the ast section that does the holidays
Problem
In my system ,the due date of the bill must be 14 days after the issued date. I have due date and I want to know issued date . I have to calculate : ``` issued date = 14 days prior to the due date ``` but 14 days must be business days ,not holidays. Holidays is stored in a table 'tblHolidayMaster' like this, Date Description 2012/05/13 Mother's Day 2012/06/02 Saturnday 2012/12/25 Christmas How can I calculate the issued date avoiding holidays? Thank you for all of your interests and replies.