编辑:对不起,我误解了你的问题。我想这就是你要找的。
这是您的 DateRange 类:
Public Class DateRange
Public Property StartTime As DateTime
Public Property EndTime As DateTime
Public ReadOnly Property NumberOfHours As Decimal
Get
Dim result As Double
result += (EndTime - StartTime).Hours
result += (EndTime - StartTime).Minutes / 60
result += (EndTime - StartTime).Seconds / 3600
Return result
End Get
End Property
End Class
这将是链接中的 C# 函数,用 VB 重写:
Private Function GetIntersectionRange(range1 As DateRange, range2 As DateRange) As DateRange
Dim iRange As New DateRange()
iRange.StartTime = If(range1.StartTime < range2.StartTime, range2.StartTime, range1.StartTime)
iRange.EndTime = If(range1.EndTime < range2.EndTime, range1.EndTime, range2.EndTime)
If iRange.StartTime > iRange.EndTime Then iRange = Nothing
Return iRange
End Function
这是一小段代码,用于执行您要求的计算并将答案写入控制台:
Dim firstDate As New DateRange With {.StartTime = New DateTime(2013, 1, 1, 8, 30, 0), .EndTime = New DateTime(2013, 1, 1, 23, 30, 0)}
Dim secondDate As New DateRange With {.StartTime = New DateTime(2013, 1, 1, 22, 0, 0), .EndTime = New DateTime(2013, 1, 2, 6, 0, 0)}
Dim result As DateRange = GetIntersectionRange(firstDate, secondDate)
Console.WriteLine(result.NumberOfHours)