得到时间段内周末的天数
1 /// <summary> 2 /// 得到一个时间段内周末的天数 3 /// </summary> 4 /// <param name="stDate">开始时间</param> 5 /// <param name="endDate">结束时间</param> 6 /// <returns></returns> 7 public double GetWeekEndTotalDays(DateTime stDate,DateTime endDate) 8 { 9 double weekEndCount = 0.000; 10 11 if (endDate.Date.ToString() == stDate.Date.ToString() && 12 (stDate.DayOfWeek.ToString() == "Saturday" || stDate.DayOfWeek.ToString() == "Sunday")) 13 { 14 weekEndCount += (endDate - stDate).TotalDays; 15 return weekEndCount; 16 } 17 18 //计算不在活动范围内的周末的天数 19 if (stDate.DayOfWeek.ToString() == "Saturday" || stDate.DayOfWeek.ToString() == "Sunday") 20 weekEndCount += (stDate.AddDays(1).Date - stDate).TotalDays; 21 stDate = stDate.AddDays(1).Date; 22 if (endDate.DayOfWeek.ToString() == "Saturday" || endDate.DayOfWeek.ToString() == "Sunday") 23 weekEndCount += (endDate - endDate.AddDays(-1).Date).TotalDays; 24 endDate = endDate.AddDays(-1).Date; 26 27 TimeSpan tsWeekEnd = endDate - stDate; 28 for (int j = 0; j <= tsWeekEnd.Days; j++) 29 { 30 if (stDate.DayOfWeek.ToString() == "Saturday" || stDate.DayOfWeek.ToString() == "Sunday") 31 { 32 weekEndCount += 1; 33 } 34 } 35 return weekEndCount; 36 }