Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何匹配不同格式的时间戳_C#_List_Timestamp - Fatal编程技术网

C# 如何匹配不同格式的时间戳

C# 如何匹配不同格式的时间戳,c#,list,timestamp,C#,List,Timestamp,我有两个存储时间戳的列表。但是两个列表中的格式不同,ListA将其存储为07/27/2015 18:10:14,而ListB将其存储为12:33。我的目标是搜索这两个列表,匹配时间戳并输出它们。我指的是相同的时间戳 假设列表A和列表B具有以下元素 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 0

我有两个存储时间戳的列表。但是两个列表中的格式不同,ListA将其存储为
07/27/2015 18:10:14
,而ListB将其存储为
12:33
。我的目标是搜索这两个列表,匹配时间戳并输出它们。我指的是相同的时间戳

假设列表A和列表B具有以下元素

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
请注意,列表B不包含秒信息,但列表A包含秒信息。我希望输出的格式如下:

07/29/2015 18:20:45     18:20
07/29/2015 18:20:11
目前,我可以执行搜索,但只能在结果中包含ListA中的一个时间戳。我想包括所有的。任何帮助都将不胜感激

我试过的方法如下:

 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");
                }
            }
        }

通过将它们解析回DateTime对象并从中提取小时和分钟,您可以准确地比较它们

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]);
        }
    }
}
for(int i=0;i

编辑:对不起,我完全误读了你的问题

这对没有时间安排的数据有效吗?选中,编辑中的示例数据。谢谢你。如果您以前面提到的格式创建了两个字符串数组(ListA和ListB),您可以对其进行测试。
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]);
        }
    }
}