私は現在、以下の関数を使用して、日付のリストから日付(今日)に最も近い日付を返しています。私の問題は、関数が今日の日付の過去または未来に関係なく、最も近い日付を返すことです。このコードを変更して、今日より後の最も近い日付と今日より前の最も近い日付を返すオプションがあるようにするにはどうすればよいですか?それは私からの地獄を混乱させています。
ご入力いただきありがとうございます。
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