Question:
C# Finding date in array of dates?
sxd3619
2011-12-15 12:40:33 UTC
If I have an array of dates and some input date. For example I have 10 dates in my array and user enters another date. I need to find the closest previous date from my input date. For example if I have an array of these 3 dates (10-dec-2007,15-dec-2007,16-dec-2007 etc) and user enters 14-dec-2007. So my closest date would be 10-dec-2007. How to accomplish this with C#. Just to give little bit more info, I am total programmer beginner and going through some tutorials. Please if somebody can help I would really appreciate it. If not thanks again for looking into my post.
Four answers:
t
2011-12-15 14:08:28 UTC
Firstly you have to parse all of the input into DateTime objects. The list of constructors can be found at http://msdn.microsoft.com/en-us/library/system.datetime.datetime.aspx .



Then compare the dates. To calculate the absolute difference between two DateTime objects, use the Subtract() method to get a TimeSpan representing the difference and then use the Duration() method to get the absolute value of that TimeSpan:



TimeSpan diff = dateOne.Subtract(dateTwo).Duration();





Here is the code for finding the best date (untested):



DateTime bestDate = array[0];

TimeSpan bestDifference = bestDate.Subtract(target).Duration();



for (int i = 0; i < array.Length; i++)

{

    TimeSpan newDifference = array[i].Subtract(target).Duration();

    if (newDifference < bestDifference)

    {

        bestDate = array[i];

        bestDifference = newDifference;

    }

}
peteams
2011-12-15 12:56:58 UTC
Firstly you need to handle having an array of zero values, a special case.



You'll probably be storing the dates as DateTime structs.



The first step is to subtract the user date from the first date in the array and to store the first date. That gets you a first guess at the nearest date and the margin of error. Subtracting one date from another gives you a TimeSpan value.



For the remainer of the dates in the array you need to subtract them from the user data. If the magnitude of the TimeSpan is smaller you need to replace your best guess so far with the newly found date and TimeSpan.



To compare the magnitudes of two TimeSpans you'll probably use code like:



if (Math.Abs(best.Ticks) > Math.Abs(next.Ticks)) {

// next is better than best, so replace best...

}
schwanebeck
2016-12-18 13:43:08 UTC
one thank you to cut up the string into areas is to apply usual expressions and the String's chop up() technique. String enter = "Jun 2 03:26:fifty 3 macbookscuba Dock[215]: Corrupt JPEG records:"; String[] input_parts = enter.chop up("\s",4); enter.chop up("\s",4) is telling Java to divide the String into an array via grabbing the 1st 4 factors separated via areas. input_parts is now an array whose individuals are: input_parts[0]: "Jun" input_parts[a million]: "2" input_parts[2]: "03:26:fifty 3" input_parts[3]: "macbookscuba Dock[215]: Corrupt JPEG records" you could desire to deliver at the same time this understanding and use conditionals to confirm which enter records is comparable to one yet another.
AJ
2011-12-15 20:19:15 UTC
Well then don't use an array.



Use List. Then you can use LINQ to query against the List like a database table.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...