2 qq 35509878 qq_35509878 于 2016.09.12 17:21 提问

求时间段内的工作日,排除双休及法定节假日,下例能排除双休,但无法排除法定节假日,该如何排除国庆节

protected void Page_Load(object sender, EventArgs e)
{
DataSet ds= new ItemQueryDataDP().GetITEMCODEByid("12");
string q=ds.Tables[0].Rows[0][0].ToString();
DateTime datet = Convert.ToDateTime("2016/9/30 12:23:23");
DateTime datet2 = Convert.ToDateTime("2016/10/30 12:23:23");
TimeSpan span = datet2.Subtract(datet);
int dayDiff = span.Days + 1;
int totleWeek = dayDiff / 7;
int yuDay = dayDiff % 7;
int lastDay = 0;
if (yuDay == 0) //正好整个周
{
lastDay = dayDiff - (totleWeek * 2);
}
else
{
int weekDay = 0;
int endWeekDay = 0; //多余的天数有几天是周六或者周日
switch (datet.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) || (weekDay == 1 && yuDay >= 7))
{
endWeekDay = 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))
{
endWeekDay = 1;
}
lastDay = dayDiff - (totleWeek * 2) - endWeekDay;
}
lblTime.Text = lastDay.ToString();
}

2个回答

simonezhlx
simonezhlx   2016.09.13 09:52

特殊节假日有时还会调整,而且是没有规律的。这些节日可以放到一个可配置的地方,作为黑名单使用即可。

qq_35509878
qq_35509878   2016.09.13 10:03

上述这个时间段中包含国庆节,由于国庆节7天,所以会包含一个双休,最后应该为16天,上述没有排除,得出的结果为21天,如何排除国庆节,已知国庆节为10.1~10.7

Csdn user default icon
上传中...
上传图片
插入图片