Reputation: 599
I have a string list holding time values in this format: 11:25:46.123
, I would like to be able to find highest and lowest time value from this list. How can I do it?
I tried something like this, but I am not sure if it is correct and I don't know what to do next.
List<TimeSpan> time = StringList.Select(x => TimeSpan.ParseExact(x, "HH:mm:ss.fff", null)).ToList();
EDIT: I am getting error:
Input string was not in a correct format.
Upvotes: 0
Views: 4537
Reputation: 2610
Try without using ParseExact
List<TimeSpan> times = StringList
.Select(x => TimeSpan.Parse(x))
.OrderBy(ts => ts)
.ToList();
TimeSpan shortest = times.First();
TimeSpan longest = times.Last();
Upvotes: 2
Reputation: 15364
I am getting error: Input string was not in a correct format.
Your timespan format is not correct. Try this
var StringList = new[] { "21:25:46.123" };
List<TimeSpan> time = StringList
.Select(x => TimeSpan.ParseExact(x, @"hh\:mm\:ss\.fff", null))
.ToList();
var max = time.Max();
var min = time.Min();
Upvotes: 7
Reputation: 10236
Try this
TimeSpan _maxtime= time.Max(); // For max time
TimeSpan _mintime= time.Min();// For min time
also take a look at MSDN
Upvotes: 4
Reputation: 68660
Have you tried:
TimeSpan maxTimeSpan = time.Max();
TimeSpan minTimeSpan = time.Min();
Upvotes: 4