-1

我正在尝试. DateTime我想将DateTime列表中存在的项目DateTime.Now最接近的日期进行比较。

到目前为止,我尝试过:DateTime.Compare(item, DateTime.Now)但这在 UiPath 中不起作用,因为我无法找到只需DATE输入 UiPath。

4

5 回答 5

1

仅用 datetime.now 给您的列表中减去日期时间有什么问题吗?它返回一个 TimeSpan 但您可以从中获取秒数(如果您想要该准确性,则一直到毫秒)。如果您希望在 DateTimes 列表中收到将来的日期时间,则只需取两者之间差异的绝对值。然后使用 linq 下单并抢到第一个。

    public static void Main(string[] args)
    {
        var now = DateTime.Now;
        var dates = new List<DateTime> 
        { 
            new DateTime(2014, 1, 1),
            new DateTime(2015, 1, 1),
            new DateTime(2016, 1, 1),
            new DateTime(2017, 1, 1),
            new DateTime(2018, 1, 1),
            new DateTime(2019, 1, 1),
            new DateTime(2020, 1, 1),
            new DateTime(2020, 4, 30),
            new DateTime(2021, 1, 1)

        };

        var minDate = dates.OrderBy(x => Math.Abs((now - x).TotalSeconds)).First();


    }
于 2020-04-30T17:50:17.657 回答
1

使用 Linq,您可能会遇到延迟评估的问题。在这种情况下,由于您使用的是时间对象和,这可能是一件非常糟糕的事情,因为在其他代码需要该值之前,DateTime.Now Linq 表达式可能不会评估返回值。这对你来说可能不是问题,其他用户已经在这篇文章中回答了 Linq 版本。否则,像下面的方法中经过验证的真正的 For 循环方法将解决问题,并返回一个 Datetime 对象以供以后使用。

public DateTime ReturnClosest(List<DateTime> dateTimes)
{
    //Establish Now for simpler code below
    var now = DateTime.Now;

    //Start by assuming the first in the list is the closest
    var closestDateToNow = dateTimes[0];

    //Set up initial interval value, accounting for dates before and after Now
    TimeSpan shortestInterval = now > dateTimes[0] ? now - dateTimes[0] : dateTimes[0] - now;

    //Loop through the rest of the list and correct closest item if appropriate
    //Note this starts at index 1, which is the SECOND value, we've evaluated the first above
    for (var i = 1; i < dateTimes.Count; i++)
    {
        TimeSpan testinterval = now > dateTimes[i] ? now - dateTimes[i] : dateTimes[i] - now;

        if(testinterval < shortestInterval)
        {
            shortestInterval = testinterval;
            closestDateToNow = dateTimes[i];
        }
    }

    //return the closest Datetime object
    return closestDateToNow;
}
于 2020-04-30T18:13:12.477 回答
0

您可以DateTime使用以下差异简单地比较两者:TimeSpan interval = DateTime.Now - otherdate;. 有了 TimeSpan 后,选择较小的

于 2020-04-30T17:36:49.917 回答
0

以一种非最佳方式,您可以使用 LINQ 来完成它,例如:

        var dates = new DateTime[]
        {
            new DateTime(2011, 5, 8),
            new DateTime(2011, 5, 9),
            new DateTime(2001, 6, 21)                
        };

        var input = new DateTime(2011, 5, 8, 13, 8, 58);
        var closest = dates
            .Select(x => new { date = x, diff = Math.Abs((x - input).Ticks)})
            .OrderBy(x => x.diff)
            .First();

        Console.WriteLine(closest.date);

输出:

5/9/2011 12:00:00 AM
于 2020-04-30T17:48:31.647 回答
0

使用 LINQ 的方法很简单,但很O(n*log(n))复杂。DateTime.Subtraction 运算符从另一个指定的日期和时间中减去指定的日期和时间,并返回一个时间间隔。TimeSpan.Duration()返回一个新的 TimeSpan 对象,其值为当前 TimeSpan 对象的绝对值。因此,您只需按持续时间排序源值并使用First().

DateTime closestDate = dates.OrderBy(d => (d - DateTime.Now).Duration()).First();

O(n)复杂的解决方案:

var closestDate = (date: new DateTime(), duration: TimeSpan.MaxValue);
foreach (var date in dates)
{
    var duration = (date - DateTime.Now).Duration();
    if (duration < closestDate.duration)
        closestDate = (date, duration);
}
return closestDate.date;

你也可以对 LINQ 和O(n)复杂性做同样的事情,但以一种不明显的方式:

DateTime closestDate = dates
    .Select(d => (date: d, duration: (d - DateTime.Now).Duration()))
    .Aggregate((date: new DateTime(), duration: TimeSpan.MaxValue), (currentClosest, d) => d.duration < currentClosest.duration ? d : currentClosest)
    .date;
于 2020-04-30T18:17:20.227 回答