Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# datetime array

I have two arrays, array1 has datetime data count by minute from 8am to 2pm and array2 has datetime data count by hour from same date 8am to 1pm.

I want to output the index number of two array that has same datetime.hour. and it should matchup the last available index number of array2 for all of the datetime data from array1 that later than array2.

for example if I have two datetime array like this:

DateTime[] dateTimes1 = new DateTime[]
        {

            new DateTime(2010, 10, 1, 8, 15, 0),
            new DateTime(2010, 10, 1, 8, 30, 1),
            new DateTime(2010, 10, 1, 8, 45, 2),
            new DateTime(2010, 10, 1, 9, 15, 3),
            new DateTime(2010, 10, 1, 9, 30, 4),
            new DateTime(2010, 10, 1, 9, 45, 5),
            new DateTime(2010, 10, 1, 10, 15, 6),
            new DateTime(2010, 10, 1, 10, 30, 7),
            new DateTime(2010, 10, 1, 10, 45, 8),
            new DateTime(2010, 10, 1, 11, 15, 9),
            new DateTime(2010, 10, 1, 11, 30, 10),
            new DateTime(2010, 10, 1, 11, 45, 11),
            new DateTime(2010, 10, 1, 12, 15, 12),
            new DateTime(2010, 10, 1, 12, 30, 13),
            new DateTime(2010, 10, 1, 12, 45, 14),
            new DateTime(2010, 10, 1, 13, 15, 15),
            new DateTime(2010, 10, 1, 13, 30, 16),
            new DateTime(2010, 10, 1, 13, 45, 17),
            new DateTime(2010, 10, 1, 14, 15, 18),
            new DateTime(2010, 10, 1, 14, 30, 19),
            new DateTime(2010, 10, 1, 14, 45, 20),
        };

        DateTime[] dateTimes2 = new DateTime[]
        {
            new DateTime(2010, 10, 1, 8, 0, 0),
            new DateTime(2010, 10, 1, 9, 0, 1),
            new DateTime(2010, 10, 1, 10, 0, 2),
            new DateTime(2010, 10, 1, 11, 0, 3),
            new DateTime(2010, 10, 1, 12, 0, 4),
            new DateTime(2010, 10, 1, 13, 0, 5),
        };

it should gives me the output:

0, 0
1, 0
2, 0
3, 1
4, 1
5, 1
6, 2
7, 2
8, 2
9, 3
10, 3
11, 3
12, 4
13, 4
14, 4
15, 5
16, 5
17, 5
18, 5
19, 5
20, 5

This is what I have tried:

            int i = 0;
            int j = 0;
            while (i < dateTimes1.Length && j < dateTimes2.Length)
            {     
                if (dateTimes1[i].Date == dateTimes2[j].Date && dateTimes1[i].Hour == dateTimes2[j].Hour)
                {
                    list.Add(i);
                    list2.Add(j);
                    i++;
                }
                else if (dateTimes1[i] < dateTimes2[j])
                {
                    i++;
                }
                else if (dateTimes1[i] > dateTimes2[j])
                {
                    j++;
                }
            }
            for (int k = 0; k < list.Count; k++)
            {
                Console.WriteLine(list[k] + " , " + list2[k];
            }

but it won't output the index number after 1pm.

like image 710
J.Lin Avatar asked Feb 09 '16 15:02

J.Lin


1 Answers

Your two lists are not the same length. In your while statement you are trying to iterate two different length lists at the same time.

If I understand your requirements properly you should be doing something like this by using an inner loop:

DateTime[] dateTimes1 = new DateTime[]
    {

        new DateTime(2010, 10, 1, 8, 15, 0),
        new DateTime(2010, 10, 1, 8, 30, 1),
        new DateTime(2010, 10, 1, 8, 45, 2),
        new DateTime(2010, 10, 1, 9, 15, 3),
        new DateTime(2010, 10, 1, 9, 30, 4),
        new DateTime(2010, 10, 1, 9, 45, 5),
        new DateTime(2010, 10, 1, 10, 15, 6),
        new DateTime(2010, 10, 1, 10, 30, 7),
        new DateTime(2010, 10, 1, 10, 45, 8),
        new DateTime(2010, 10, 1, 11, 15, 9),
        new DateTime(2010, 10, 1, 11, 30, 10),
        new DateTime(2010, 10, 1, 11, 45, 11),
        new DateTime(2010, 10, 1, 12, 15, 12),
        new DateTime(2010, 10, 1, 12, 30, 13),
        new DateTime(2010, 10, 1, 12, 45, 14),
        new DateTime(2010, 10, 1, 13, 15, 15),
        new DateTime(2010, 10, 1, 13, 30, 16),
        new DateTime(2010, 10, 1, 13, 45, 17),
        new DateTime(2010, 10, 1, 14, 15, 18),
        new DateTime(2010, 10, 1, 14, 30, 19),
        new DateTime(2010, 10, 1, 14, 45, 20),
    };

    DateTime[] dateTimes2 = new DateTime[]
    {
        new DateTime(2010, 10, 1, 8, 0, 0),
        new DateTime(2010, 10, 1, 9, 0, 1),
        new DateTime(2010, 10, 1, 10, 0, 2),
        new DateTime(2010, 10, 1, 11, 0, 3),
        new DateTime(2010, 10, 1, 12, 0, 4),
        new DateTime(2010, 10, 1, 13, 0, 5),
    };

    int i = 0;
    while (i < dateTimes1.Length)
    {
        int j = 0;
        while (j < dateTimes2.Length))
        {
            if (dateTimes1[i].Date == dateTimes2[j].Date && dateTimes1[i].Hour == dateTimes2[j].Hour)
            {
                list.Add(i);
                list2.Add(j);
                i++;
            }
            else if (dateTimes1[i] < dateTimes2[j])
            {
                i++;
            }
            else if (dateTimes1[i] > dateTimes2[j])
            {
                j++;
            }
        }
    }
    for (int k = 0; k < list.Count; k++)
    {
        Console.WriteLine(list[k] + " , " + list2[k];
    }       
like image 152
Andrew Truckle Avatar answered Sep 26 '22 06:09

Andrew Truckle