2012-02-20 146 views
1

嗨,我是c#和編程一般我一直在嘗試學習適應其他人的代碼下面的代碼我已經改變了代碼,因爲我想要計算的金額在一個月內剩餘的工作日,但問題是代碼運行在一個月的天數,所以這個月有29天但代碼錯誤代碼運行到30日我找不出哪個部分代碼更改任何幫助將是巨大的c#如何計算一個月剩餘的工作日

private void days() 
    { 

     //Monday to Friday are business days. 
     var weekends = new DayOfWeek[] { DayOfWeek.Saturday, DayOfWeek.Sunday }; 
     DateTime testing = DateTime.Now; 



     string month1 = testing.ToString("M "); 
     string year1 = testing.ToString("yyyy"); 
     int month = Convert.ToInt32(month1); 
     int year = Convert.ToInt32(year1); 


     //Fetch the amount of days in your given month. 
     int daysInMonth = DateTime.DaysInMonth(year, month); 
     string daysleft = testing.ToString("d "); 
     int daystoday = Convert.ToInt32(daysleft); 

     //Here we create an enumerable from 1 to daysInMonth, 
     //and ask whether the DateTime object we create belongs to a weekend day, 
     //if it doesn't, add it to our IEnumerable<int> collection of days. 
     IEnumerable<int> businessDaysInMonth = Enumerable.Range(daystoday, daysInMonth) 
               .Where(d => !weekends.Contains(new DateTime(year, month, d).DayOfWeek)); 

     //Pretty smooth. 

     int count = 0; 
     foreach (var day in businessDaysInMonth) 
     { 
      count = count + 1; 
     } 
     textBox9.Text = count.ToString(); 


    } 
} 

回答

9
public static IEnumerable<int> Range(int start, int count) 

正如你所看到的第二個參數不是結束,而是計數。

你想要的數量大概是:daysInMonth - daystoday + 1

我重寫代碼爲:

private static readonly DayOfWeek[] weekends = new DayOfWeek[] { DayOfWeek.Saturday, DayOfWeek.Sunday }; 

bool IsWorkDay(DateTime day)//Encapsulate in a function, to simplify dealing with holydays 
{ 
    return !weekends.Contains(day.DayOfWeek); 
} 

int WorkDaysLeftInMonth(DateTime currentDate) 
{ 
    var remainingDates = Enumerable.Range(currentDate.Day,DateTime.DaysInMonth(currentDate.Year,currentDate.Month)-currentDate.Day+1) 
         .Select(day=>new DateTime(currentDate.Year, currentDate.Month, day)); 
    return remainingDates.Count(IsWorkDay); 
} 
4

有沒有需要你的日期轉換爲字符串,然後解析組件回整數值。查看DateTime「測試」的「月」,「日」和「年」屬性。「

(CodeInChaos有你的答案,但是這是一個有趣的事實,這將大大簡化你的代碼。)

0
private int GetWorkingDaysLeftInMonth() 
    { 
     // get the daysInMonth 
     int daysInMonth = GetDaysInMonth(); 

     // locals 
     int businessDaysInMonth = 0; 
     int day = DateTime.Now.Day; 
     bool isWeekDay = false; 

     int currentDay = (int) DateTime.Now.DayOfWeek; 
     DayOfWeek dayOfWeek = (DayOfWeek)currentDay; 

     // iterate the days in month 
     for (int x = day; x < daysInMonth; x++) 
     { 
      // increment the current day 
      currentDay++; 

      // if the day is greater than 7 
      if (currentDay > 7) 
      { 
       // reset the currentDay 
       currentDay = 1; 
      } 

      // get the dayOfWeek 
      dayOfWeek = (DayOfWeek) currentDay; 

      switch(dayOfWeek) 
      { 
       case DayOfWeek.Monday: 
       case DayOfWeek.Tuesday: 
       case DayOfWeek.Wednesday: 
       case DayOfWeek.Thursday: 
       case DayOfWeek.Friday: 

        // is a week day 
        isWeekDay = true; 

        // required 
        break; 

       default: 

        // is a NOT week day 
        isWeekDay = true; 

        // required 
        break; 
      } 

      if (isWeekDay) 
      { 
       // increment the value 
       businessDaysInMonth++; 
      } 
     } 

     // return value 
     return businessDaysInMonth; 
    } 

    private int GetDaysInMonth() 
    { 
     // initial value 
     int daysInMonth = 0; 

     switch(DateTime.Now.Month) 
     { 
      case 1: 
      case 3: 
      case 5: 
      case 7: 
      case 8: 
      case 10: 
      case 12: 

       daysInMonth = 31; 

       // required; 
       break; 

      case 2: 

       daysInMonth = 28; 

       // to do (leap year) 
       bool isLeapYear = IsLeapYear(); 

       // if isLeapYear 
       if (isLeapYear) 
       { 
        // set to 29 
        daysInMonth = 29; 
       } 

       // required 
       break; 

      case 4: 
      case 6: 
      case 9: 
      case 11: 

       daysInMonth = 30; 

       // required; 
       break; 
     } 

     // return value 
     return daysInMonth; 
    } 

    private bool IsLeapYear() 
    { 
     // initial value 
     bool isLeapYear = false; 

     int year = DateTime.Now.Year; 

     //determine the year 
     switch(year) 
     { 
      case 2012: 
      case 2016: 
      case 2020: 
      // to do: Go as far out as you need to 

       // set to true 
       isLeapYear = true; 

       // required 
       break; 
     } 

     // return value 
     return isLeapYear; 
    } 
+0

你並不需要實現'GetDaysInMonth'自己。該框架已經包含「DateTime.DaysInMonth」。你的閏年邏輯很醜陋(它也重複了框架代碼)。您可以使用模運算符來計算任意年份的跳躍。 – CodesInChaos 2012-02-20 22:50:26

+0

我在5分鐘內寫出來表達如何去做,而不是最有效的方法。 – user1054326 2012-02-21 01:27:57