2011-11-14 5 views
-2

이제는 두 개의 endDate (코드에서 10,15) 분의 값이 같은 startDate (25)에 대해 발생합니다.여러 날짜 차이가있는 코드 #

해결 방법 : 나는이 두 분 중에서 최소의 가치를 원합니다. 충돌하는 endDate에 의해 주어진 값. 나는 귀하의 질문에 제대로 나는 당신이 뭘 하려는지 이해하는 관리 생각되게되고에 위의 신사에 동의 않지만

public class DateController : Controller 
    public ActionResult date() 
    { 
     int allDiff; 
     List<int> list=new List<int>(); 
     int flag = 0; 

     DateTime[] startDate = new DateTime[3]; 
     startDate[0] = new DateTime(2011, 11, 5); 
     startDate[1] = new DateTime(2011, 11, 7); 
     startDate[2] = new DateTime(2011, 11, 25); 

     DateTime[] endDate = new DateTime[3]; 
     endDate[0] = new DateTime(2011, 11, 10); 
     endDate[1] = new DateTime(2011, 11,15); 
     endDate[2] = new DateTime(2011, 11, 30); 
     DateTime Min= startDate.Min(); 
     DateTime Max = endDate.Max(); 

     TimeSpan span = Max - Min; 
     int total = span.Days; 
     ViewBag.globalTotal = total; 


     foreach (DateTime e in endDate) 
     { 

      foreach (DateTime s in startDate) 
      { 
       if (s >= e) 
       { 
        TimeSpan span1 = s - e; 
        allDiff = span1.Days; 
        list.Add(allDiff); 
        flag = 1; 

       } 
       else { 
        flag = 0; 
       } 

      } 
      if (flag == 1) 
      { 
       int m = list.Min(); 
       ViewBag.dhiraj = m; 
       total = total - m; 
       list.Clear(); 
      } 

     } 
     ViewBag.Total = total; 

     return View(); 
    } 
+5

1 : 여기에 게시 된 모든 사람에게 도움이 필요합니다 .-) 2 : 귀하의 질문에 약간의 혼란이 있습니다. 너 정말 알고 싶어? – Fischermaen

+0

여러 날짜 차이를 계산하고 싶습니다 ... diff에 겹치지 않는 날짜와 갭 b/w 두 개의 날짜가 포함되는 것을 포함합니다. startDate = 2011/11/05 및 2011/11/25 endDate = 2011/11/10 및 2011/11/20 출력은 10 일이어야합니다. – RollerCosta

+0

다른 고려 사항 :: 겹침 ... startDate : 2011/11/05 및 2011/11/07 endDate : 2011/11/10 및 2011/11/15 필요 OUTPUT 10days – RollerCosta

답변

0

작업 코드

public ActionResult date() 
    { 
     int j = 0; 
     int output = 0; 
     DateTime[] startDate = new DateTime[6]; 

     startDate[0] = new DateTime(2011, 11, 1); 
     startDate[1] = new DateTime(2011, 11, 6); 
     startDate[2] = new DateTime(2011, 11, 16); 
     startDate[3] = new DateTime(2011, 11, 17); 
     startDate[4] = new DateTime(2011, 11, 17); 
     startDate[5] = new DateTime(2011, 11, 17); 




     DateTime[] endDate = new DateTime[6]; 
     endDate[0] = new DateTime(2011, 11, 4); 
     endDate[1] = new DateTime(2011, 11, 8); 
     endDate[2] = new DateTime(2011, 11, 18); 
     endDate[3] = new DateTime(2011, 11, 17); 
     endDate[4] = new DateTime(2011, 11, 22); 
     endDate[5] = new DateTime(2011, 11, 19); 

     //5-7 
               //7-9 
               //15-20 
     List<DateTime> start = new List<DateTime>(); 
     List<DateTime> end = new List<DateTime>(); 

     DateTime interStart = default(DateTime); 
     DateTime interEnd = default(DateTime);//initialize 
     start.Add(interStart); 
     end.Add(interEnd); 



     for (int i = 0; i < startDate.Length ; i++) 
     { 
      if(i < (startDate.Length-1)){ 
       j=i+1; 
      } 
      else{ 
       j = i; 
      } 

      if ((startDate[i] <= endDate[j]) && (endDate[i] >= startDate[j])) 
      { 


       List<DateTime> intermediateStart = new List<DateTime>(); 
       intermediateStart.Add(startDate[i]); 
       intermediateStart.Add(startDate[j]); 
       interStart = intermediateStart.Min(); 
       List<DateTime> intermediateEnd = new List<DateTime>(); 
       intermediateEnd.Add(endDate[i]); 
       intermediateEnd.Add(endDate[j]); 
       interEnd = intermediateEnd.Max(); 


       if ((start.Last() <= interEnd) && (end.Last() >= interStart)) 
       { 

         List<DateTime> finalStartValue = new List<DateTime>(); 
         finalStartValue.Add(start.Last()); 
         finalStartValue.Add(interStart); 
         List<DateTime> finalEndValue = new List<DateTime>(); 
         finalEndValue.Add(end.Last()); 
         finalEndValue.Add(interEnd); 

         DateTime finalStart = finalStartValue.Min(); 
         DateTime finalEnd = finalEndValue.Max(); 


         start.Remove(start.Last()); 
         end.Remove(end.Last()); 
         start.Add(finalStart); 
         end.Add(finalEnd); 
       } 
       else 
       { 
        start.Add(interStart); 
        end.Add(interEnd); 
       } 

      } 
      else 
      { 
       start.Add(startDate[i]); 
       end.Add(endDate[i]); 
       //remove(start.first); 
      } 
     } 
     int count=(start.Count())-1; 
     for (int k = 0; k <= count; k++) { 
      TimeSpan span = end[k]-start[k]; 
      int diff = span.Days; 
      output = diff + output; 

     } 


      ViewBag.finalOutput = output; 

      return View(); 
    } 
}} 
0

, 다음과 같이 루프 당신이 대체 :

//Create list of startdates that are greater than any of the end dates 
//and return the differences in days 
var list = from s in startDate 
      from e in endDate 
      where s > e 
      select (s-e).Days; 

//If any were found, take out the min of those 
int dhiraj = list.Count() == 0 ? 0 : list.Min(); 
//Update total 
total -= dhiraj; 
+0

어디에서 '총'이란 무엇입니까? – V4Vendetta

+0

@ V4Vendetta 전체 코드를 정의하는 코드는 계속됩니다. 앞에서 말한 것처럼이 코드 블록은 이중 foreach 루프 만 대체합니다. – edvaldig

관련 문제