在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
/// <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; }
|
请发表评论