how to categorise the given set of datetime in C#

66 Views Asked by At

I have a list of datetime values and i need to group them into hours base. something like

enter image description here

2

There are 2 best solutions below

0
On

You can convert Your time formats in 100 conversion

select convert(varchar, GetDate(), 100) as '100 Conversion'

Possible Result : Aug 27 2013 6:13PM Use string manipulation like substring to retrieve the number 6 and PM

Do some Logic to sort the retrieve Data.

after that your problem is done, Hope this Helps Thanks.

4
On

You can use linq:

        var times = new List<DateTime>();

        var groups = times.GroupBy(date => (baseTime - date).TotalHours);

        foreach (var @group in groups)
        {
            Debug.WriteLine("hours: " + group.Key);
            foreach (var dateTime in group)
            {
                Debug.WriteLine(dateTime);
            }
        }

baseTime is the time from where you want to start off