【问题标题】:Given a list of dates, how do I get the nearest date in the past to today and the nearest date in the future to today in VB.NET给定日期列表,如何在 VB.NET 中获取过去到今天的最近日期和未来到今天的最近日期
【发布时间】:2012-09-03 01:32:15
【问题描述】:

我目前正在使用下面的函数将日期列表中最近的日期返回到日期(今天)。我的问题是,该函数返回最接近的日期,无论它是今天日期的过去还是未来。如何更改此代码,以便可以选择返回今天之后的最近日期和今天之前的最近日期?这让我很困惑。

非常感谢您的意见。

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

【问题讨论】:

    标签: vb.net date datetime timespan


    【解决方案1】:

    看起来这就是你要找的东西。您只需要能够将某些内容传递给函数,以便它知道您想要什么。为了明确起见,我选择了一个枚举。我还更新了它以传回一个可为空的日期。这应该可以解决您的时间问题,但您需要考虑返回 null 值。

    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
    

    【讨论】:

    • 嘿,感谢您的回复,但它似乎对我不起作用,GreaterThanEqualTo 有效,但 LessThanEqualTo 似乎一直返回 00:00:00,有什么想法吗?
    【解决方案2】:

    希望您可以将其转换为 VB.Net。思路是对日期进行排序,然后找到给定日期的索引,那么集合中的上一个日期和下一个日期分别是过去和未来最近的日期

    查找最近的日期

    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;
    }
    

    查找最近的日期

    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;
    }
    

    从上图中,选择任意日期,上一个和下一个是最近的日期。请注意,日期已排序。例如,选择 8 月 15 日,那么最近的未来日期是 21,最近的过去日期是 12

    【讨论】:

    • 您好,感谢您的回复,我确实使用在线工具进行了转换,但是它没有给出任何错误,它返回的值是错误的,future 什么也不返回,prev 似乎返回一个值那太远了:(把我搞糊涂了
    • 让我试试新版本:)
    • 是的,似乎是同样的问题,当列表中有许多 2012 日期时,它会返回 2008 年的值作为最近的 After,而对于最近的之前它什么也不返回。 :(
    • 你能给我样品日期吗?希望您致电ld.Sort()
    • 是的,我做了,示例日期只是日期,即 2012 年 2 月 31 日、2011 年 1 月 28 日等
    猜你喜欢
    • 1970-01-01
    • 2016-03-28
    • 1970-01-01
    • 1970-01-01
    • 2021-02-04
    • 1970-01-01
    • 1970-01-01
    • 2018-06-06
    • 2021-07-30
    相关资源
    最近更新 更多