我目前使用下面的函数从列表中返回最近的日期约会日期(今天)。我的问题是,该函数返回最近的日期,无论它是过去或今天的日期的未来。如何更改此代码,以便今天之后可以选择返回距离最近的日期和距离最近的日期?这让我很困惑。给定一个日期列表,我如何获得过去到今天的最近日期以及将来到今天最近的日期在VB.NET中
非常感谢您的意见。
Function GetNearestDate(ByVal source As IEnumerable(Of DateTime), ByVal target As DateTime) As DateTime
Dim result As DateTime = Nothing
Dim lowestDifference = TimeSpan.MaxValue
For Each _date As DateTime In source
If _date >= target Then
Continue For
End If
Dim difference = target - _date
If difference < lowestDifference Then
lowestDifference = difference
result = _date
End If
Next
Return result
End Function
嘿谢谢回复,但它似乎并没有为我工作,GreaterThanEqualTo的作品,但LessThanEqualTo似乎一直返回00:00:00,任何想法? – Nookster
只是更新了我的文章 – UnhandledExcepSean
非常感谢很多这个工程伟大 – Nookster