Given a list of dates, how do I get the nearest da

2019-08-14 12:33发布

问题:

I am currently using the function below to return the nearest date from a list of dates to a date(today). My problem is, the function returns the closest date regardless of it being the past or the future of todays date. How can I change this code so there is an option to return the nearest date AFTER today and the nearest date BEFORE today? It's confusing the hell out of me.

Thanks a lot for your input.

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

回答1:

Seems like this is what you are looking for. you simply need to be able to pass something in to the function so it knows what you want. I chose an enum for explicitness. I also updated it to pass back a nullable date. This should correct your time issues, but you'll need to account for null returned values.

Public Enum DateCompare
    LessThanEqualTo
    GreaterThanEqualTo
End Enum

Public Function GetNearestDate(ByVal source As IEnumerable(Of DateTime), _
                               ByVal target As DateTime, _
                               ByVal dt As DateCompare) As Nullable(Of DateTime)
    Dim result As Nullable(Of DateTime) = Nothing
    Dim lowestDifference As TimeSpan = TimeSpan.MaxValue
    Dim difference As TimeSpan

    For Each _date As DateTime In source
        If dt = DateCompare.LessThanEqualTo And _date > target Then
            Continue For
        ElseIf dt = DateCompare.GreaterThanEqualTo And _date < target Then
            Continue For
        End If

        If target > _date Then
            difference = target - _date
        Else
            difference = _date - target
        End If

        If difference < lowestDifference Then
            lowestDifference = difference
            result = _date
        End If
    Next

    Return result
End Function


回答2:

Hope you can convert this to VB.Net. The idea is to sort the date, then find the index of the given date, then the previous date and the next date in the collection is the nearest in the past and future respectively

find nearest date after

string findNearestAfter(List<DateTime> ld, DateTime t)
{
    ld.Sort();

    int index = 0;
    for (int i = 0; i < ld.Count; i++ )
    {
        if (ld[i] == t)
            index = i;
    }

    string nearest = "";

    if (index < ld.Count)
        nearest = ld[index + 1].ToString();

    return nearest;
}

find nearest date before

string findNearestBefore(List<DateTime> ld, DateTime t)
{
    ld.Sort();

    int index = 0;
    for (int i = 0; i < ld.Count; i++ )
    {
        if (ld[i] == t)
            index = i;
    }

    string nearest = "";

    if (index > 0)
        nearest = ld[index - 1].ToString();

    return nearest;
}

From the Image above, pick any date, the previous and next are the nearest dates. Note the dates are sorted. For example, picking 15 of August, then nearest future date is 21 and the nearest past date is 12