datetime最低值查找

时间:2017-12-27 12:24:54

标签: c# .net datetime

我有一些DateTime值。如何从值中选择最低日期以及哪个数组包含该值。

DateTime file1date = DateTime.ParseExact(fileListfordiff[0].Substring(22, 8), "yyyyMMdd", CultureInfo.InvariantCulture);
DateTime file2date = DateTime.ParseExact(fileListfordiff[1].Substring(22, 8), "yyyyMMdd", CultureInfo.InvariantCulture);
DateTime file3date = DateTime.ParseExact(fileListfordiff[2].Substring(22, 8), "yyyyMMdd", CultureInfo.InvariantCulture);
DateTime file4date = DateTime.ParseExact(fileListfordiff[3].Substring(22, 8), "yyyyMMdd", CultureInfo.InvariantCulture);
DateTime file5date = DateTime.ParseExact(fileListfordiff[4].Substring(22, 8), "yyyyMMdd", CultureInfo.InvariantCulture);
DateTime file6date = DateTime.ParseExact(fileListfordiff[5].Substring(22, 8), "yyyyMMdd", CultureInfo.InvariantCulture);

4 个答案:

答案 0 :(得分:1)

我建议您也使用数组作为文件日期。 这也使您可以在填充数组时获得最低值:

var filesDate = new DateTime[fileListfordiff.Length];
var lowestDate = DateTime.MaxValue;
var lowestDateIndex = -1;
for(int i=0; i < fileListfordiff.Length; i++)
{
    filesDate[i] = DateTime.ParseExact(fileListfordiff[i].Substring(22, 8), "yyyyMMdd", CultureInfo.InvariantCulture);
    if(filesDate[i] < lowestDate)
    {
        lowestDate = filesDate[i];
        lowestDateIndex = i;
    }
}

答案 1 :(得分:0)

首先创建一个indexPath.section数组。将值保存到数组,然后您可以使用LINQ获得最低日期。

 NSIndexSet *section = [NSIndexSet indexSetWithIndex:indexPath.section];
[self.tableView reloadSections:section withRowAnimation:UITableViewRowAnimationNone];

或者考虑一个您不需要保存值的情况,只需找到最低的日期。

DateTime

答案 2 :(得分:0)

如果您愿意使用MoreLINQ,请考虑以下方法:

var fileDate = new List<DateTime>();
for (int i = 0; i <= 5; i++)
{
    fileDate.Add(DateTime.ParseExact(fileListfordiff[i].Substring(22, 8), "yyyyMMdd", CultureInfo.InvariantCulture));
}

var arrayIndex = fileDate
    .Select((value, index) => new {index, value})
    .MinBy(z => z.value).index;

Select调用允许您同时获取值和索引,MinBy允许您获取具有最低值的条目。

答案 3 :(得分:-1)

您可以执行类似

的操作
List<DateTime> fileDate = new List<DateTime>();
for(int i=0;i<=5;i++)
{
   fileDate.Add(DateTime.ParseExact(fileListfordiff[i].Substring(22, 8), "yyyyMMdd", CultureInfo.InvariantCulture));
}
DateTime minVal = fileDate.Min();
int minIndex = fileDate.IndexOf(minVal);
相关问题