public static class DateTimeExtensions { ////// Includes the from and to dates so from Monday to Tuesday it returns 2 /// public static int FindBusinessDaysUntil(this DateTime from, DateTime to) { double calcBusinessDays = 1 + ((to - from).TotalDays * 5 - (from.DayOfWeek - to.DayOfWeek) * 2) / 7; if ((int)to.DayOfWeek == 6) calcBusinessDays--; if ((int)from.DayOfWeek == 0) calcBusinessDays--; return Convert.ToInt16(calcBusinessDays); } public static DateTime NextBusinessDay(this DateTime from) { var nextDay = from.AddDays(1); while (true) { var numberOfBusinessDays = FindBusinessDaysUntil(from, nextDay); if (numberOfBusinessDays == 2) { return nextDay; } nextDay = nextDay.AddDays(1); } } public static DateTime NextBusinessDay(this DateTime from, int days) { var result = from; for (int i = 0; i < days; i++) { result = result.NextBusinessDay(); } return result; } } }
Thursday 7 July 2016
DateTime Extensions
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment