2012-02-20 13 views
1

こんにちは、私はc#に新規です。一般的に私は、他の人々のコードを次のコードに適合させることから学びようとしています。 1月に作業日数が残っていますが、問題はコードが月の日数を超えて実行されるため、今月は29日ですが、コードとしてのコードエラーはに実行されます。私はどの部分すべてのヘルプは素晴らしい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) 
だろう変更するコードの

あなたが見ることができるように、2番目のパラメータは終了ではなく、カウントです。

したいカウントはおそらくです:

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

文字列にあなたの日付を変換してから戻って整数値の成分を解析する必要はありません:としてdaysInMonth - daystoday + 1

私はあなたのコードを書き換えたいです。 DateTimeの "Month"、 "Day"、 "Year"のプロパティを確認してください。

(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

+0

私はそれを行う方法を表現するために5分で書いたが、それを行う最も効率的な方法ではなかった。 – user1054326

関連する問題