how to get mindate ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • aboon
    New Member
    • Aug 2008
    • 12

    how to get mindate ?

    Hi Guys,
    I've been trying to get the Mindate from the array. But the output I've got was all dates in the array. I know there is somthing missing but I couldn't figure it out. Please help.
    Thanks
    aboon.

    Code:
    DateTime dtMinDate = new DateTime();
                string strDateFrom = emp.DateFrom; 
                string [] arrDateFrom = strDateFrom.Split((new char[]{','})); 
                if (arrDateFrom != null && arrDateFrom.Length > 0)            
                {   
                    for (int j = 0; j < arrDateFrom.Length; j++)                
                    {    
    	    dtMinDate  = Convert.ToDateTime(arrDateFrom.GetValue(j));
                     }
                     Response.Write("Minimum Date: " + dtMinDate.Date.ToString());
                 }
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    All yo uare doing is printing out the dates in the array.
    You are not doing any logic on it to determine which date is smallest at all.
    Perhaps you should trying doing some comparison logic on it?

    Comment

    • balabaster
      Recognized Expert Contributor
      • Mar 2007
      • 798

      #3
      Ah, I came across a scenario where I wanted to provide the max from a param array of doubles earlier and I was half way through coding a loop when I thought of a couple of other nifty ways of doing it that would work here too that didn't require me looping through the array...and is also much less code.

      The first is with LINQ
      Code:
      Dim qry = From n In MyParamArray Order By n Select n
      Dim min = qry.First
      Dim max = qry.Last
      The second is just using the plain old sort mechanism from the Array construct:
      Code:
      Dim Sorted = Array.Sort(MyParamArray)
      Dim min = Sorted(0)
      Dim max = Sorted(Sorted.Length - 1)
      I imagine the same two concepts could be applied to dates without any fuss.

      Comment

      • aboon
        New Member
        • Aug 2008
        • 12

        #4
        That's the part I'm not sure how to do. I haved tried the code below, the out put of mindate was the DateTime.MinDat e not the mindate in the array. Would you plaease give show me some example.

        Code:
        DateTime dtMinDate = new DateTime();
        string strDateFrom = empWH.DateFrom;
        string [] arrDateFrom = strDateFrom.Split((new char[]{','})); 
        		
        if (arrDateFrom != null && arrDateFrom.Length > 0)            
        {   
           for (int j = 0; j < arrDateFrom.Length; ++j)
          {
              DateTime dtFrom1 = Convert.ToDateTime(arrDateFrom.GetValue(j));
              for (int k = j + 1; k < arrDateFrom.Length; ++k)
              {
                  DateTime dtFrom2 = Convert.ToDateTime(arrDateFrom.GetValue(k));
                  int intFrom = DateTime.Compare(dtFrom1, dtFrom2);
                  if (intFrom > 0)
                  {
                      dtMinDate = dtFrom2;
                      break;
                  }
              }
          }
        }
        Response.Write("Minimum Date: " + dtMinDate.Date.ToString());

        Comment

        • Curtis Rutland
          Recognized Expert Specialist
          • Apr 2008
          • 3264

          #5
          balabaster has a good suggestion. If you have an array of DateTimes, you should be able to .Sort() the array no problem. Then, depending on how the sort algorithm is set up, it will either be the first or last value in the array. Most likely the first.

          Comment

          • PRR
            Recognized Expert Contributor
            • Dec 2007
            • 750

            #6
            Try checking out
            using System.Collecti ons;
            using System.Collecti ons.Generic;

            They are better ways to store up info....

            Code:
            List<DateTime> now = new List<DateTime>();
            
            now.Add(DateTime.Now);
                        now.Add(DateTime.Now.Subtract(new TimeSpan(1,0,0,0)));
                        now.Add(DateTime.Now.AddDays(1));
            
                        now.Sort();
            //sorted...
            
                        now.Reverse();
            // now in reverse order..

            Comment

            Working...