VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > Python基础教程 >
  • C#教程之C#获取两个时间的时间差并去除周末(取

本文实例讲述了C#获取两个时间的时间差并去除周末的方法。分享给大家供大家参考。具体分析如下:

一般来说取时间差的代码很多,但是能够只取工作日的时间差的代码很少,这段代码就来实现这一功能。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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#程序设计有所帮助。


相关教程