• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

C#取两个时间的时间差并去除周末

OC/C/C++ 水墨上仙 2611次浏览

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#取两个时间的时间差并去除周末
喜欢 (0)
加载中……