C#獲得兩個時光的時光差並去除周末(取任務日)的辦法。本站提示廣大學習愛好者:(C#獲得兩個時光的時光差並去除周末(取任務日)的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是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#法式設計有所贊助。