/// <summary> /// Returns the next working day (Mon-Fri, not public holiday) /// after the specified date (or the same date) /// </summary> /// <param name="holidayCalendar">The holiday calendar.</param> /// <param name="dt">The date you wish to check</param> /// <returns> /// A date that is a working day /// </returns> public static DateTime NextWorkingDay(IPublicHolidays holidayCalendar, DateTime dt) { bool isWorkingDay = false; //loops through while (isWorkingDay == false) { //Mon-Fri and not bank holiday, it's okay if (dt.DayOfWeek != DayOfWeek.Saturday && dt.DayOfWeek != DayOfWeek.Sunday && !holidayCalendar.IsPublicHoliday(dt)) isWorkingDay = true; //it's Saturday, so skip to Monday else if (dt.DayOfWeek == DayOfWeek.Saturday) dt = dt.AddDays(2); //it's Sunday, so skip to Monday else if (dt.DayOfWeek == DayOfWeek.Sunday) dt = dt.AddDays(1); //it's Friday (bank holiday), so skip to Monday else if (dt.DayOfWeek == DayOfWeek.Friday) dt = dt.AddDays(3); //it's Mon-Thu (bank holiday), so next day else dt = dt.AddDays(1); //any of the addDays should now loop and retest //only Good Friday and Christmas Day should loop twice //(2 adjacent bank holidays) } return dt; }
/// <summary> /// Returns the next working day (Mon-Fri, not public holiday) /// after the specified date (or the same date) /// </summary> /// <param name="holidayCalendar">The holiday calendar.</param> /// <param name="dt">The date you wish to check</param> /// <returns> /// A date that is a working day /// </returns> public static DateTime NextWorkingDay(IPublicHolidays holidayCalendar, DateTime dt) { bool isWorkingDay = false; dt = dt.Date; //we don't care about time part //loops through while (isWorkingDay == false) { //Mon-Fri and not bank holiday, it's okay if (dt.DayOfWeek != DayOfWeek.Saturday && dt.DayOfWeek != DayOfWeek.Sunday && !holidayCalendar.IsPublicHoliday(dt)) { isWorkingDay = true; } //it's Saturday, so skip to Monday else if (dt.DayOfWeek == DayOfWeek.Saturday) { dt = dt.AddDays(2); } //it's Sunday, so skip to Monday else if (dt.DayOfWeek == DayOfWeek.Sunday) { dt = dt.AddDays(1); } //it's Friday (bank holiday), so skip to Monday else if (dt.DayOfWeek == DayOfWeek.Friday) { dt = dt.AddDays(3); } //it's Mon-Thu (bank holiday), so next day else { dt = dt.AddDays(1); } //any of the addDays should now loop and retest //only Good Friday and Christmas Day should loop twice //(2 adjacent bank holidays) } return(dt); }
/// <summary> /// Returns the previous working day (Mon-Fri, not public holiday) /// before the specified date (or the same date) /// </summary> /// <param name="holidayCalendar">The holiday calendar.</param> /// <param name="dt">The date you wish to check</param> /// <returns> /// A date that is a working day /// </returns> public static DateTime PreviousWorkingDay(IPublicHolidays holidayCalendar, DateTime dt) { bool isWorkingDay = false; dt = dt.Date; //we don't care about time part //loops through while (isWorkingDay == false) { //Mon-Fri and not bank holiday, it's okay if (dt.DayOfWeek != DayOfWeek.Saturday && dt.DayOfWeek != DayOfWeek.Sunday && !holidayCalendar.IsPublicHoliday(dt)) { isWorkingDay = true; } //it's Sunday, so skip to Friday else if (dt.DayOfWeek == DayOfWeek.Sunday) { dt = dt.AddDays(-2); } //it's Saturday, so skip to Friday else if (dt.DayOfWeek == DayOfWeek.Saturday) { dt = dt.AddDays(-1); } //it's Monday (bank holiday), so skip to Friday else if (dt.DayOfWeek == DayOfWeek.Monday) { dt = dt.AddDays(-3); } //it's Thi-Fr (bank holiday), so previous day else { dt = dt.AddDays(-1); } } return(dt); }
/// <summary> /// Returns whether the specified date is a working day /// </summary> /// <param name="holidayCalendar">The holiday calendar.</param> /// <param name="dt">The date to be checked</param> /// <returns>Returns a boolean of whether the specified date is a working day</returns> public static bool IsWorkingDay(IPublicHolidays holidayCalendar, DateTime dt) { return(dt.DayOfWeek != DayOfWeek.Saturday && dt.DayOfWeek != DayOfWeek.Sunday && !holidayCalendar.IsPublicHoliday(dt)); }
/// <summary> /// Returns the previous working day (Mon-Fri, not public holiday) /// before the specified date (or the same date) /// </summary> /// <param name="holidayCalendar">The holiday calendar.</param> /// <param name="dt">The date you wish to check</param> /// <returns> /// A date that is a working day /// </returns> public static DateTime PreviousWorkingDay(IPublicHolidays holidayCalendar, DateTime dt) { bool isWorkingDay = false; //loops through while (isWorkingDay == false) { //Mon-Fri and not bank holiday, it's okay if (dt.DayOfWeek != DayOfWeek.Saturday && dt.DayOfWeek != DayOfWeek.Sunday && !holidayCalendar.IsPublicHoliday(dt)) isWorkingDay = true; //it's Sunday, so skip to Friday else if (dt.DayOfWeek == DayOfWeek.Sunday) dt = dt.AddDays(-2); //it's Saturday, so skip to Friday else if (dt.DayOfWeek == DayOfWeek.Saturday) dt = dt.AddDays(-1); //it's Monday (bank holiday), so skip to Friday else if (dt.DayOfWeek == DayOfWeek.Monday) dt = dt.AddDays(-3); //it's Thi-Fr (bank holiday), so previous day else dt = dt.AddDays(-1); } return dt; }