简体   繁体   中英

How to match timestamps of different formats

I have two lists that store timestamps within them. However the format in both lists is different, ListA stores it as 07/27/2015 18:10:14 while ListB stores it as 12:33 . My objective is to search both lists and match timestamps and output them. By same timestamps, I mean..

Lets say List A and List B have the following elements...

List A:                 List B:
07/27/2015 18:10:14     18:11   
07/29/2015 18:20:45     18:20
07/29/2015 18:20:11     19:23
07/11/2015 18:21:23     20:45

Note that ListB does not contain seconds information but List A does. I want the output in the format below:

07/29/2015 18:20:45     18:20
07/29/2015 18:20:11

At the moment, I am able to perform the search but can only include one timestamp from ListA in my results. I want to include all. Any help will be appreciated!

What I have tried is below:

 for (int i = 0; i < ListA.Count(); i++)
        {
            for (int j = 0; j < ListB.Count(); j++)
            {
                if (ListA[i].Substring[11,5] == ListB[j])
                {
                    Console.WriteLine("Match Found");
                }
            }
        }

EDIT::

List A:                 List B:
07/27/2015 18:10:14     18:11   
07/29/2015 18:20:45     20:45
07/29/2015 18:20:11     19:23
07/11/2015 18:21:23     18:20

By parsing them back to DateTime objects and taking the hours and minutes from each, you can compare them accurately

for (int i = 0; i < ListA.Count(); i++)
{
    bool found = false;

    for (int j = 0; j < ListB.Count(); j++)
    {
        DateTime aItem = DateTime.ParseExact(ListA[i], "MM/dd/yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture);
        DateTime bItem = DateTime.ParseExact(ListB[j], "HH:mm", System.Globalization.CultureInfo.InvariantCulture);

        if (aItem.ToString("HH:mm") == bItem.ToString("HH:mm"))
        {
              if (!found)
              {
                  Console.WriteLine("{0}    {1}", ListA[i], ListB[j]);
                  found = true;
              }
              else
                  Console.WriteLine(ListA[i]);
        }
    }
}

Edit: sorry I misread your question completely

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM