简体   繁体   中英

How to find lowest and highest time from list?

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.

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();

Have you tried:

TimeSpan maxTimeSpan = time.Max();
TimeSpan minTimeSpan = time.Min();

Try this

TimeSpan _maxtime= time.Max(); // For max time
TimeSpan _mintime= time.Min();// For min time

also take a look at MSDN

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM