Csharp/C#教程:如何在给定日期前14天避免假期分享


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

在我的系统中,账单的到期日必须在签发日期后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 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 holidaySet; public ReadOnlyCollection WeekendDays{get; private set;} public BusinessDays(IEnumerable holidays, IEnumerable weekendDays) { WeekendDays = new ReadOnlyCollection(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(holidays); } public BusinessDays(IEnumerable 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。)

使其成为您想要的function或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

请尝试以下链接,

https://www.c-sharpcorner.com/uploadfile/tirthacs/difference-between-two-dates-excluding-weekends/

上述就是C#学习教程:如何在给定日期前14天避免假期分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注---计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/1027886.html

(0)
上一篇 2022年1月10日
下一篇 2022年1月10日

精彩推荐