How to compare DateTime Objects while looping through a list?

Posted by Taniq on Stack Overflow See other posts from Stack Overflow or by Taniq
Published on 2012-06-07T09:29:35Z Indexed on 2012/06/07 10:41 UTC
Read the original article Hit count: 190

Filed under:
|
|

I'm trying to loop through a list (csv) containing two fields; a name and a date. There are various duplicated names and various dates in the list. I'm trying to deduce for each name in the list, where there are multiple instances of the same name, which corresponding date is the latest.

I realise, from looking at another answer, that I need to use the DateTime.Compare method which is fine, but my problem is working out which date is later. Once I know this I need to produce a file with unique names and the latest date relating to it.

This is my first question which makes me a newbie.

EDIT:

Initially I thought it would be 'ok' to set the LatestDate object to a date that wouldn't show up in my file, therefore making any later dates in the file the LatestDate.

Here's my coding so far:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace flybe_overwriter
{
class Program
{
    static DateTime currentDate;
    static DateTime latestDate = new DateTime(1000,1,1);
    static HashSet<string> uniqueNames = new HashSet<string>();

    static string indexpath = @"e:\flybe test\indexing.csv";
    static string[] indexlist = File.ReadAllLines(indexpath);
    static StreamWriter outputfile = new StreamWriter(@"e:\flybe test\match.csv");

    static void Main(string[] args)
    {

        foreach (string entry in indexlist)
        {

            uniqueNames.Add(entry.Split(',')[0]);

        }

        HashSet<string>.Enumerator fenum = new HashSet<string>.Enumerator();
        fenum = uniqueNames.GetEnumerator();

        while (fenum.MoveNext())
        {
            string currentName = fenum.Current;


            foreach (string line in indexlist)
            {
                currentDate = new DateTime(Convert.ToInt32(line.Split(',')[1].Substring(4, 4)), 
                                           Convert.ToInt32(line.Split(',')[1].Substring(2, 2)), 
                                           Convert.ToInt32(line.Split(',')[1].Substring(0, 2)));

                if (currentName == line.Split(',')[0])
                { 
                    if(DateTime.Compare(latestDate.Date, currentDate.Date) < 1)
                    {
                      //  Console.WriteLine(currentName + " " + latestDate.ToShortDateString() + " is earlier than " + currentDate.ToShortDateString());
                    }
                    else if (DateTime.Compare(latestDate.Date, currentDate.Date) > 1)
                    {
                     //   Console.WriteLine(currentName + " " + latestDate.ToShortDateString() + " is later than " + currentDate.ToShortDateString());
                    }
                    else if (DateTime.Compare(latestDate.Date, currentDate.Date) == 0)
                    {
                     // Console.WriteLine(currentName + " " + latestDate.ToShortDateString() + " is the same as " + currentDate.ToShortDateString());
                    }

                }
            }

        }


    }
}

}

Any help appreciated. Thanks.

© Stack Overflow or respective owner

Related posts about c#

Related posts about datetime