2014-01-16 71 views
-1

我想計算去年的未使用假期並將其顯示在網格列中。我知道我必須做一個簡單的計算如何檢查去年

,如:

VacationDays - UsedVacationDays ==從去年

但剩下的假期我該怎麼查,我只收到從去年的數據?我知道,必須有一些與DateTime.Today.AddYears(-1),至少我覺得洙..

到目前爲止我的代碼:

public double UnusedVacationDays 
     { 

      get 
      { 
       return (this.VacationDays - this.UsedVacationDays); 
      } 

     } 

編輯

public double GetUsedVacation(int year) 
     { 
      double hours = 0; 

      IEnumerable<HtVacationDay> vacationDays = this.HtVacationDays.Where(x => x.FromDate.Year == year && x.FromDate < DateTime.Today); 
      foreach (HtVacationDay vacation in vacationDays) 
      { 
       TimeSpan span = vacation.ToDate - vacation.FromDate; 
       if (span.TotalHours > 8) 
       { 
        hours += span.TotalHours/3; 
       } 
       else 
       { 
        hours += span.TotalHours; 
       } 
      } 

      return hours/8; 

感謝您的幫助! ^^

+0

因此,你有兩個雙精度值(例如'12.0'和'6.5'),並且想要找到它們去年的哪一部分? –

+1

檢查日期的'.Year'屬性? –

+0

@SergeyBerezovskiy是的! – TamoDaleko

回答

0

在計算休假小時,這屬於你的一年的使用日期範圍:

public double GetUsedVacation(int year) 
{ 
    double hours = 0; 

    IEnumerable<HtVacationDay> vacationDays = 
     HtVacationDays.Where(x => x.FromDate.Year == year && 
            x.FromDate < DateTime.Today); 

    DateTime firstDayOfYear = new DateTime(year, 1, 1); 
    DateTime lastDayOfYear = new DateTime(year, 12, 31); 

    foreach (HtVacationDay vacation in vacationDays) 
    { 
     DateTime from = vacation.FromDate.Date < firstDayOfYear ? 
       firstDayOfYear : vacation.FromDate; 

     DateTime to = vacation.ToDate.Date > lastDayOfYear ? 
       lastDayOfYear : vacation.ToDate; 

     var vacationHours = (to - from).TotalHours;    

     if (vacationHours > 8)    
      hours += vacationHours/3;    
     else    
      hours += vacationHours;    
    } 

    return hours/8; 
} 

而且我相信小時計算應要複雜得多。因爲假期在15:00開始,第二天在13:00結束,您的代碼將返回總共22個小時。除以3得出的結果小於3小時的假期。但實際上如果我們的工作時間從上午9點到下午6點,它應該給5 + 4 = 9小時的假期。