主要思路:计算日期差过了几周,就用日期差除7得到周数,在判断开始日期和结束日期存在周末的情况,最终得到的结果=总日期差-周数*2-结束日期和开始日期所在周末的次数
#region 计算除去周末的日期 var totleWeek = AllDays / 7; //经过的周数 var yuDay = totleWeek % 7; if (yuDay == 0) //正好整周 { days = AllDays - (totleWeek * 2); } else { var weekDay = 0; var Week= 0; //多余的天数有几天是周六或者周日 switch (start.DayOfWeek) { case DayOfWeek.Monday: weekDay = 1; break; case DayOfWeek.Tuesday: weekDay = 2; break; case DayOfWeek.Wednesday: weekDay = 3; break; case DayOfWeek.Thursday: weekDay = 4; break; case DayOfWeek.Friday: weekDay = 5; break; case DayOfWeek.Saturday: weekDay = 6; break; case DayOfWeek.Sunday: weekDay = 7; break; } if ((weekDay == 6 && yuDay >= 2) || (weekDay == 7 && yuDay >= 1) || (weekDay == 5 && yuDay >= 3) || (weekDay == 4 && yuDay >= 4) || (weekDay == 3 && yuDay >= 5) || (weekDay == 2 && yuDay >= 6)) { Week = 2; } if ((weekDay == 6 && yuDay < 1) || (weekDay == 7 && yuDay < 5) || (weekDay == 5 && yuDay < 2) || (weekDay == 4 && yuDay < 3) || (weekDay == 3 && yuDay < 4) || (weekDay == 2 && yuDay < 5) || (weekDay == 1 && yuDay < 6)) { Week = 1; } days = AllDays - (totleWeek * 2) - Week; } #endregion