如何在给定日期前14天避免假期

时间:2012-06-07 10:01:00

标签: c# sql-server-2008

在我的系统中,账单的到期日必须在签发日期后14天 我有截止日期,我想知道签发日期 我必须计算:

issued date = 14 days prior to the due date

14天必须是工作日,而不是假期。
假期存储在表'tblHolidayMaster'中,如下所示,

  
    

日期说明     
2012/05/13母亲     天
2012/06/02 Saturnday
2012年12月25日圣诞节

  

如何计算避免假期的发布日期?
感谢您的所有兴趣和回复。

5 个答案:

答案 0 :(得分:3)

我会使用类似下面的函数(我使用的)

来计算日期
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);

}

没有测试过假期的ast部分

答案 1 :(得分:2)

我选择了直接循环解决方案,因此长时间间隔会很慢。但是对于像14天这样的短暂间隔,它应该非常快。

您需要在构造函数中传递假期。 BusinessDays的实例是不可变的,可以重用。在实践中,您可能会使用IoC单例或类似的构造来获取它。

如果开始日期是非工作日,则

AddBusinessDays会抛出ArgumentException,因为您没有指定如何处理该案例。特别是非工作日的AddBusinessDays(0)会产生奇怪的属性。它要么打破时间逆转对称,要么返回非工作日。

public class BusinessDays
{
    private HashSet<DateTime> holidaySet;
    public ReadOnlyCollection<DayOfWeek> WeekendDays{get; private set;}

    public BusinessDays(IEnumerable<DateTime> holidays, IEnumerable<DayOfWeek> weekendDays)
    {
        WeekendDays = new ReadOnlyCollection<DayOfWeek>(weekendDays.Distinct().OrderBy(x=>x).ToArray());
        if(holidays.Any(d => d != d.Date))
            throw new ArgumentException("holidays", "A date must have time set to midnight");
        holidaySet = new HashSet<DateTime>(holidays);
    }

    public BusinessDays(IEnumerable<DateTime> holidays)
        :this(holidays, new[]{DayOfWeek.Saturday, DayOfWeek.Sunday})
    {
    }

    public bool IsWeekend(DayOfWeek dayOfWeek)
    {
        return WeekendDays.Contains(dayOfWeek);
    }

    public bool IsWeekend(DateTime date)
    {
        return IsWeekend(date.DayOfWeek);
    }

    public bool IsHoliday(DateTime date)
    {
        return holidaySet.Contains(date.Date);
    }

    public bool IsBusinessDay(DateTime date)
    {
        return !IsWeekend(date) && !IsHoliday(date);
    }

    public DateTime AddBusinessDays(DateTime date, int days)
    {
        if(!IsBusinessDay(date))
            throw new ArgumentException("date", "date bust be a business day");
        int sign = Math.Sign(days);
        while(days != 0)
        {
            do
            {
              date.AddDays(sign);
            } while(!IsBusinessDay(date));
            days -= sign;
        }
        return date;
    }
}

答案 2 :(得分:1)

我认为这就是你所需要的。这很简单,我已经测试了它并且它正在工作......在数据库中编写函数或SP而不是在C#中编写复杂代码并不是一种糟糕的方法...(更改日期中的列日期名称)分贝。)

使其成为您想要的功能或SP。

注意:评论“星期六”和“星期天”的检查。如果它已经添加到表reocrds中。

declare @NextWorkingDate datetime
declare @CurrentDate datetime
set @CurrentDate = GETDATE()
set @NextWorkingDate = @CurrentDate 
declare @i int = 0
While(@i < 14)
Begin
    if(((select COUNT(*) from dbo.tblHolidayMaster where convert(varchar(10),[Date],101) like convert(varchar(10),@NextWorkingDate,101)) > 0) OR DATENAME(WEEKDAY,@NextWorkingDate) = 'Saturday' OR DATENAME(WEEKDAY,@NextWorkingDate) = 'Sunday')
    Begin
        print 'a '  
        print @NextWorkingDate
        set @NextWorkingDate = @NextWorkingDate + 1
        CONTINUE
    End
    else
    Begin
        print  'b ' 
        print @NextWorkingDate
        set @NextWorkingDate = @NextWorkingDate + 1
        set @i = @i + 1
        CONTINUE
    End
End
print @NextWorkingDate 

答案 3 :(得分:1)

我只从你的'tblHolidayMaster'表中计算发布日期以避免您的假期。

 int addDay = -14;
  DateTime dtInputDay = System.DateTime.Now;//Your input day
  DateTime dtResultDate = new DateTime();
  dtResultDate = dtInputDay.AddDays(addDay);
  bool result = false;
  string strExpression;
  DataView haveHoliday;
  while (!result) {
      strExpression = "Date >='" + Convert.ToDateTime(dtResultDate.ToString("yyyy/MM/dd")) + "' and Date <='" + Convert.ToDateTime(dtInputDay.ToString("yyyy/MM/dd")) + "'";
      haveHoliday = new DataView(tblHolidayMaster);
      haveHoliday.RowFilter = strExpression;
      if (haveHoliday.Count == 0) {          
        result = true;
      } else {
          addDay = -(haveHoliday.Count);
          dtInputDay = dtResultDate.AddDays(-1);
          dtResultDate = dtResultDate.AddDays(addDay);
      }
  }                      

您的签发日期是dtResultDate

答案 4 :(得分:0)