So, here's the scenario. I have a file with a created time, and I want to choose a time from a list of times that that file's created time is closest or equal too...what would be the best way to accomplish this?
Finding the future closest date to today in Excel 1. Select the blank cell B2, copy and paste formula =MIN(IF(A2:A18>TODAY(),A2:A18)) into the Formula Bar, and then press Ctrl + Shift + Enter keys simultaneously. See screenshot: Then you will get the future closest date to today in cell B2.
Abs((date - targetDate). Ticks)); var nearest = getAlldates. Where(date => Math. Abs((date - targetDate).
var closestTime = listOfTimes.OrderBy(t => Math.Abs((t - fileCreateTime).Ticks)) .First();
If you don't want the performance overhead of the OrderBy
call then you could use something like the MinBy
extension method from MoreLINQ
instead:
var closestTime = listOfTimes.MinBy(t => Math.Abs((t - fileCreateTime).Ticks));
Something like this:
DateTime fileDate, closestDate; ArrayList theDates; long min = long.MaxValue; foreach (DateTime date in theDates) if (Math.Abs(date.Ticks - fileDate.Ticks) < min) { min = Math.Abs(date.Ticks - fileDate.Ticks); closestDate = date; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With