检查不同日期的时间范围

时间:2018-07-23 19:33:09

标签: vb.net

我有一个开始和结束日期,如以下示例所示:

Dim start as DateTime = "26-07-2017 21:36:40"
Dim [end] as DateTime = "27-07-2017 06:35:37"

我要检查在此时间范围内是否存在02:24H(在这种情况下存在)。 如果开始和结束是同一天,这将很容易,但是在发生日期变化的情况下进行此验证的最简单,最有效的方法是什么?

1 个答案:

答案 0 :(得分:0)

Function TimeExists(ByVal start As DateTime, ByVal [end] As DateTime, ByVal time As TimeSpan) As Boolean
    'You only need to check two days: the 'start' day and the 'start + 1' day

    Select Case start.Date + time
        Case start To [end] : Return True
    End Select

    Select Case start.Date.AddDays(1) + time
        Case start To [end] : Return True
    End Select
End Function