Dark Knight Dark Knight - 1 year ago 66
C# Question

How to get the Latest DATETIME in my collection

My collection

"10/01/2016 4:00 PM"
"10/01/2016 11:00 AM"
"10/01/2016 12:00 PM"

I want to get : "10/01/2016 4:00 PM" since it is the current time and the latest at all. However when i use .Max() Function it returns the value of "10/01/2016 12:00 PM" which means it is just looking for the Highest Value of HOURS "12".

Answer Source

You comparing string values.

You need to convert your collection of strings to the collection of DateTime

var dateTimeCollection = stringCollection.Select(value => DateTime.Parse(value));

Then use Max method

var maxDate = dateTimeCollection.Max();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download