当前位置: 首页 > 编程语言 > C#

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

时间:2023-04-10 11:44:29 C#

如何在给定日期前14天避假在我的系统中,账单的到期日必须在出票日后14天。我有一个截止日期,我想知道发布日期。我必须计算:签发日期=到期日之前14天,但14天必须是工作日,而不是假期。假期存储在这样一个表'tblHolidayMaster'中,日期描述2012/05/13母亲节2012/06/02星期六2012/12/25圣诞节如何计算发布日期避免假期?感谢您的关注和回复。我会使用以下函数(我使用的)来计算日期publicstaticDateTimeAddBusinessDays(DateTimedate,intdays){if(days==0)returndate;如果(date.DayOfWeek==DayOfWeek.Saturday){date=date.AddDays(2);天数-=1;}elseif(date.DayOfWeek==DayOfWeek.Sunday){date=date.AddDays(1);天数-=1;}date=date.AddDays(天/5*7);intextraDays=天数%5;如果((int)date.DayOfWeek+extraDays>5){extraDays+=2;}intextraDaysForHolidays=-1;//将数据库中的假期加载到列表中Listdates=GetHolidays();while(extraDaysForHolidays!=0){vardays=dates.Where(x=>x>=date&&x}还没有测试假期的最后一部分我选择了直接循环解决方案,所以长间隔会很慢。但对于像14天这样的短时间间隔,它应该非常快。你需要在构造函数中传递假期。BusinessDays的实例是不可变的,可以重复使用。实际上,您可能会使用IoC单例或类似结构来获取它。如果开始日期是非工作日,AddBusinessDays将抛出ArgumentException,因为您没有指定如何处理这种情况。特别是非工作日的AddBusinessDays(0)会有奇怪的属性。它要么打破时间反转对称性,要么返回非工作日。公共类BusinessDays{privateHashSetholidaySet;公共ReadOnlyCollectionWeekendDays{get;私有集;}publicBusinessDays(IEnumerableholidays,IEnumerableweekendDays){WeekendDays=newReadOnlyCollection(weekendDays.Distinct().OrderBy(x=>x).ToArray());if(holidays.Any(d=>d!=d.Date))thrownewArgumentException("holidays","Adatemusthavetimesettomidnight");holidaySet=newHashSet(假期);}publicBusinessDays(IEnumerableholidays):this(holidays,new[]{DayOfWeek.Saturday,DayOfWeek.Sunday}){}publicboolIsWeekend(DayOfWeekdayOfWeek){returnWeekendDays.Contains(dayOfWeek);}publicboolIsWeekend(DateTimedate){returnIsWeekend(date.DayOfWeek);}publicboolIsHoliday(DateTimedate){returnholidaySet.Contains(date.Date);}publicboolIsBusinessDay(DateTimedate){return!IsWeekend(date)&&!IsHoliday(date);}publicDateTimeAddBusinessDays(DateTimedate,intdays){if(!IsBusinessDay(date))thrownewArgumentException("date","datebustbeabusinessday");intsign=Math.Sign(days);while(days!=0){do{date.AddDays(sign);}while(!IsBusinessDay(date));天-=符号;}归期;我认为这就是您所需要的,它很简单,我已经测试过它并且可以正常工作...在数据库中而不是在C#中编写函数或SP在...中编写复杂代码不是一个坏方法...(更改列日期名称在日期)Db。)使其成为您想要的功能或SP。注意:在“星期六”和“星期日”审查检查。如果它已被添加到您的表reocrds中。声明@NextWorkingDate日期时间声明@CurrentDate日期时间set@CurrentDate=GETDATE()set@NextWorkingDate=@CurrentDatedeclare@iint=0While(@i0)ORDATENAME(WEEKDAY,@NextWorkingDate)='Saturday'ORDATENAME(WEEKDAY,@NextWorkingDate)='星期日')Beginprint'a'print@NextWorkingDateset@NextWorkingDate=@NextWorkingDate+1CONTINUEEndelseBeginprint'b'print@NextWorkingDateset@NextWorkingDate=@NextWorkingDate+1set@i=@i+1CONTINUEEndEndprint@NextWorkingDate我只计算你的表“tblHolidayMaster”中的发布日期以避免你的假期。intaddDay=-14;DateTimedtInputDay=System.DateTime.Now;//你输入的日期DateTimedtResultDate=newDateTime();dtResultDate=dtInputDay.AddDays(addDay);布尔结果=假;字符串strExpression;数据视图有假期;while(!result){strExpression="Date>='"+Convert.ToDateTime(dtResultDate.ToString("yyyy/MM/dd"))+"'andDateYourissuedateisdtResultDate请尝试以下链接,http://www.c-sharpcorner.com/uploadfile/tirthacs/difference-between-two-dates-excluding-weekends/以上就是C#学习教程的全部内容:Howtoavoidholidaysharing14daysbeforeagivendate,如果对大家有用,需要了解更多C#学习教程,希望大家多多关注---本文收集自网络,不代表立场,如涉及侵权,请点击右转联系管理员删除,如需转载请注明出处: