简体   繁体   English

如何从列表中找到最低和最高时间?

[英]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. 我有一个字符串列表,其中包含以下格式的时间值: 11:25:46.123 ,我希望能够从此列表中找到最高和最低时间值。 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 也看看MSDN

Try without using ParseExact 尝试不使用ParseExact

List<TimeSpan> times = StringList
    .Select(x => TimeSpan.Parse(x))
    .OrderBy(ts => ts)
    .ToList();
TimeSpan shortest = times.First();
TimeSpan longest = times.Last();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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