在我的系统,该法案的到期日必须在发出日期后14天内。
我有到期日和我想知道发行日期。
我要计算:
issued date = 14 days prior to the due date
但有14天必须是工作日,节假日不。
假期存储在像这样的表“tblHolidayMaster”,
日期说明
2012/05/13母亲节
2012/06/02 Saturnday
2012年12月25日圣诞节
我如何计算的发行日期间避免假期?
感谢您对所有您的利益和回复。
我会使用类似下面的函数(我使用)计算日期
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节不放假
我去的直线前进循环的解决方案,所以这将是长的时间间隔慢。 但对于短的间隔像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;
}
}
我认为这是你需要什么。 这很简单,我已经测试过它,这是工作......而且这还不是写在数据库中的函数或SP,而写在C#中复杂的代码个不错的办法......(日期的变化列名作为你的D b。)
令其功能或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
我计算发行日期间避免你的表“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
试试下面的链接,
http://www.c-sharpcorner.com/uploadfile/tirthacs/difference-between-two-dates-excluding-weekends/