C#获取两个时间的时间差并去除周末(取工作日)的方法
程序员文章站
2022-07-01 17:11:44
本文实例讲述了c#获取两个时间的时间差并去除周末的方法。分享给大家供大家参考。具体分析如下:
一般来说取时间差的代码很多,但是能够只取工作日的时间差的代码很少,这段代码就...
本文实例讲述了c#获取两个时间的时间差并去除周末的方法。分享给大家供大家参考。具体分析如下:
一般来说取时间差的代码很多,但是能够只取工作日的时间差的代码很少,这段代码就来实现这一功能。
protected void page_load(object sender, eventargs e) { datetime start = convert.todatetime("2012-12-10"); datetime end= convert.todatetime("2012-12-18"); timespan span = end - start; //int totleday=span.days; //datetime spannu = datetime.now.subtract(span); int alldays=convert.toint32(span.totaldays)+1;//差距的所有天数 int totleweek = alldays / 7;//差别多少周 int yuday = alldays % 7; //除了整个星期的天数 int lastday = 0; if (yuday == 0) //正好整个周 { lastday = alldays - (totleweek * 2); } else { int weekday = 0; int endweekday = 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) || (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 = alldays - (totleweek * 2) - endweekday; } lbltime.text = lastday.tostring(); }
希望本文所述对大家的c#程序设计有所帮助。
上一篇: [翻译] .NET Core 3.0 RC 1 发布
下一篇: 夏季健康饮食 大暑时节要打开胃口