C# 获取一段日期内的工作日
/// <summary> /// 根据指定时间段计算工作日天数 /// </summary> /// <param name="firstDay"></param> /// <param name="lastDay"></param> /// <param name="bankHolidays"></param> /// <returns></returns> public static int BusinessDaysUntil(DateTime firstDay, DateTime lastDay, params DateTime[] bankHolidays) { firstDay = firstDay.Date; lastDay = lastDay.Date; if (firstDay > lastDay) throw new ArgumentException("最后一天不正确" + lastDay); TimeSpan span = lastDay - firstDay; int businessDays = span.Days + 1; int fullWeekCount = businessDays / 7; if (businessDays > fullWeekCount * 7) { int firstDayOfWeek = firstDay.DayOfWeek == DayOfWeek.Sunday ? 7 : (int)firstDay.DayOfWeek; int lastDayOfWeek = lastDay.DayOfWeek == DayOfWeek.Sunday ? 7 : (int)lastDay.DayOfWeek; if (lastDayOfWeek < firstDayOfWeek) lastDayOfWeek += 7; if (firstDayOfWeek <= 6) { if (lastDayOfWeek >= 7) businessDays -= 2; else if (lastDayOfWeek >= 6) businessDays -= 1; } else if (firstDayOfWeek <= 7 && lastDayOfWeek >= 7) businessDays -= 1; } businessDays -= fullWeekCount + fullWeekCount; foreach (DateTime bankHoliday in bankHolidays) { DateTime bh = bankHoliday.Date; if (firstDay <= bh && bh <= lastDay) --businessDays; } return businessDays; }
有错误的请多多指教,共同进步(๑•ᴗ•๑)
By听雨的人
By听雨的人